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 8082609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T17:10:14+00:00 2026-06-05T17:10:14+00:00

Is there a way to determine the type of variable passed to a template

  • 0

Is there a way to determine the type of variable passed to a template and call a function based on if it’s an int or std::string etc…?

For example

template <class T>
struct Jam
{
     Jam(T *var)
     {
         if (typeid(var) == typeid(std::string*)
                *var = "Hello!";
         else if (typeid(var) == typeid(int*)
                *var = 25;
     }
};

When I try to use that code, i get an error invalid conversion from const char* to int. I suspect this is because the compiler “expands” the template into separate functions and when I specified a new instance of the structure throw Jam<std::string>(&setme); it detected the var* = 25 statement and refused to compile.

Is there a proper way to do this? Maybe with macro guards? Thanks.

  • 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-05T17:10:15+00:00Added an answer on June 5, 2026 at 5:10 pm

    Use regular function overloading instead:

    template <class T>
    struct Jam
    {
        Jam(std::string* var)
        {
            *var = "Hello!";
        }
    
        Jam(int* var)
        {
            *var = 25;
        }
    };
    

    unless you want to specialize on the type T used to instantiate Jam. In that case you would do:

    template<>
    struct Jam<std::string>
    {
        Jam(std::string* var)
        {
            *var = "Hello!";
        }
    };
    
    template<>
    struct Jam<int>
    {
        Jam(int* var)
        {
            *var = 25;
        }
    };
    
    
    template<typename T>
    struct Jam
    {
        Jam(T* var)
        {
            // every other type
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any way to determine the type of a variable passed as an
Is there a way to programmatically determine the type(/name) of the underlying file system?
Is there a way to determine the exception type even know you caught the
I have a jQuery selection, is there a way to determine what type of
I'm looking for a similar IsStructure function. Is there some other way to determine
Is there a way to determine the Object type, when passing a reference to
Is there a way to determine what file type was used to create a
Is there a way to determine the type of an element within a list
Is there any way to determine the local culture of a PC (such as
Is there a way to determine programmatically, using Python, which web page is currently

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.