Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8620949
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:42:16+00:00 2026-06-12T06:42:16+00:00

everyone! I have a template function named UpdateValue, which was designed to update some

  • 0

everyone!

I have a template function named UpdateValue, which was designed to update some values with different types such as integer, BOOL, string, and so on. Please see the following code snippet for your reference:

#include <typeinfo.h>

template<class T> 
void UpdateValue(T Value)
{   
    if ( typeid(int) == typeid(Value) ) 
    {  
       ZOrder(Value);
    }
    else if ( typeid(bool) == typeid(Value) )
    {  
       BOOL bShow = Value ? TRUE : FALSE;
       Show(bShow);
    }
    else if ( typeid(CString) == typeid(Value) )
    {  
       Theme(Value);
    }
}

void Show(BOOL bShow) { m_bShow = bShow; }
void ZOrder(int nZOrder) { m_nZOrder = nZOrder; }
void Theme(CString strTheme) { m_strTheme = strTheme; }

BOOL m_bShow;
int m_nZOrder;
CString m_strTheme;

But when I use the following statements to call this kind of template function

CString strValue = _T("Animal");
UpdateValue<CString>(strValue);

the aforesaid code failed to compile by throwing the following exceptions:

e:\dynaprop\dynaprop\mainfrm.cpp(269) : error C2664: ‘CMainFrame::ZOrder’ : cannot convert parameter 1 from ‘CString’ to ‘int’
1> No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1> e:\dynaprop\dynaprop\mainfrm.cpp(67) : see reference to function template instantiation ‘void CMainFrame::UpdateValue(T)’ being compiled
1> with
1> [
1> T=CString
1> ]

Would you please show me how to figure it out? Thank you in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T06:42:17+00:00Added an answer on June 12, 2026 at 6:42 am

    You are not really using the template facilities. Or rather miss using it. What happens is you call the ZOrder(Value) the void UpdateValue(T Value) get instantiated where every occurence of T gets replaced with CString as you specified. But that is all.

    The function look like this

    void UpdateValue(CString Value)
    {   
        if ( typeid(int) == typeid(Value) ) 
        {  
           ZOrder(Value);
        }
        else if ( typeid(bool) == typeid(Value) )
        {  
           BOOL bShow = Value ? TRUE : FALSE;
           Show(bShow);
        }
        else if ( typeid(CString) == typeid(Value) )
        {  
           Theme(Value);
        }
    }
    

    After the template gets instantied the normal compiling stuff happens … and you get an error ’cause it is written ZOrder(Value); where value is of CString type. This will produce an error, even though utilizing RTTI this call path should never be reached in such case.

    What you should do is to use specializations to handle different types. Try replacing original definition of UpdateValue with these:

    template<> // specialization when T is get to be int
    void UpdateValue<int>(int Value) { ZOrder(Value);};
    
    template<> // specialization when T is get to be CString
    void UpdateValue<bool>(bool Value) { BOOL bShow = Value ? TRUE : FALSE; Show(bShow);};
    
    template<> // specialization when T is get to be CString
    void UpdateValue<CString>(CString Value) { Theme(Value);};
    
    template<class T>  //general case
    void UpdateValue(T Value) { cout << "IMPLEMENT ME\n";};
    

    Then when you call UpdateValue< T > you invoke version with appropriate body. also you don’t have to use RTTI.

    UPDATE: After the commenter suggestion. If you do not need/want to the handle general case (i.e. unknown type), you can resort to plain overloads and go with:

    //Value is int
    void UpdateValue(int Value) { ZOrder(Value);};
    //Value is bool
    void UpdateValue(bool Value) { BOOL bShow = Value ? TRUE : FALSE; Show(bShow);};
    //Value is CString
    void UpdateValue(CString Value){ Theme(Value);};
    

    No need even for templates then!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello everyone, I have some confusion regarding some Win32 API data types and macros
everyone. I have some labels that I draw them in the xib file, and
i have one website which is working on templates. in the template there is
Hello everyone I have a problem. I want to do a GridView with a
Have a nice day everyone, I have something to ask your hel, to better
I have a central git repository that everyone pushes to for testing and integration,
everyone! I have sprite moving by action, what have health bar (progress bar). When
Hi everyone my problem today is I have a couple of buttons that slide
Hello everyone Masters Of Web Delevopment :) I have a piece of PHP script
I have those code snippet that checks the fans table and gets everyone who

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.