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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:52:01+00:00 2026-06-07T14:52:01+00:00

If I want to make a template class, and depending on the typeid of

  • 0

If I want to make a template class, and depending on the typeid of the template parameter perform different actions, then how do I code this?

For instance, I have the following template class, in which I want to initialize the member field data depending on whether it is an int or a string.

#include <string>

template <class T>
class A
{
private:
    T data;
public:
    A();
};

// Implementation of constructor
template <class T>
A<T>::A()
{
    if (typeid(T) == typeid(int))
    {
        data = 1;
    }
    else if (typeid(T) == typeid(std::string))
    {
        data = "one";
    }
    else
    {
        throw runtime_error("Choose type int or string");
    }
}

This code would not compile however, with the following main file.

#include "stdafx.h"
#include "A.h"
#include <string>

int _tmain(int argc, _TCHAR* argv[])
{
    A<int> one;
    return 0;
}

The error is: error C2440: ‘=’ : cannot convert from ‘const char [2]’ to ‘int’, which means the code is actually checking the else-if statement for an int, even though it will never be able to reach that part of the code.

Next, following this example (Perform different methods based on template variable type), I tried the following A.h file, but I got several linker errors mentioning that A(void) is already defined in A.obj.

#include <string>

template <class T>
class A
{
private:
    T data;
public:
    A();
    ~A();
};

// Implementation of constructor
template <>
A<int>::A()
{
    data = 1;
}
template <>
A<std::string>::A()
{
    data = "one";
}

Does anybody know how to get this code up and running? I also realize that using such an if-else statement in a template class might remove the power from a template. Is there a better way to code this?

EDIT: after discussion with Torsten (below), I now have the following A.h file:

#pragma once

#include <string>

// Class definition
template <class T>
class A
{
public:
    A();
    ~A();
private:
    T data;
};

// Implementation of initialization
template < class T > 
struct initial_data
{
  static T data() { throw runtime_error("Choose type int or string"); }
};

template <> 
struct initial_data< int >
{
    static int data() { return 1; }
};

template <> 
struct initial_data< std::string >
{
    static std::string data() { return "one"; }
};

// Definition of constructor
template <class T>
A<T>::A()
  : data( initial_data< T >::data() ) 
{
}

and the following main:

#include "stdafx.h"
#include "A.h"
#include <string>

int _tmain(int argc, _TCHAR* argv[])
{
    A<int> ione;

    return 0;
}

The linker error I now get is: Test template 4.obj : error LNK2019: unresolved external symbol “public: __thiscall A::~A(void)” (??1?$A@H@@QAE@XZ) referenced in function _wmain

  • 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-07T14:52:02+00:00Added an answer on June 7, 2026 at 2:52 pm

    Explicit specializations are the way to go.

    I assume that you are including your A.h in several .cpp, and that’s the root cause of your problem.

    Specializations are definitions and there must be only one definition of A::A() and A::A() and so they must be in only one .cpp.

    You’ll have to move the explicit specialization in a .cpp

    template <>
    A<int>::A()
    {
        data = 1;
    }
    template <>
    A<std::string>::A()
    {
        data = "one";
    }
    

    and keep a declaration for them in A.h

    template<> A<int>::A();
    template<> A<std::string>::A();
    

    so that the compiler knows they are explicitly specialized and doesn’t try to add automatic one.

    Edit: with these four files, g++ m.cpp f.cpp a.cpp doesn’t show any errors.

    // a.h
    #define A_H
    
    #include <string>
    
    template <class T>
    class A
    {
    private:
        T data;
    public:
        A();
    };
    
    template<> A<int>::A();
    template<> A<std::string>::A();
    
    #endif
    
    // a.cpp
    #include "a.h"
    
    template <>
    A<int>::A()
    {
        data = 1;
    }
    template <>
    A<std::string>::A()
    {
        data = "one";
    }
    
    // f.cpp
    #include "a.h"
    
    int f()
    {
        A<int> one;
        A<std::string> two;
    }
    
    // m.cpp
    #include "a.h"
    
    int f();
    
    int main()
    {
        A<int> one;
        A<std::string> two;
        f();
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a template class, what I want to do are the following Make
I want to make a class that will have a single get template method
I have style sheet with a class name changebackgroundcolor i want make change in
I want to make this code work properly, what should I do? giving this
Consider this code: template<typename T> class Base { template<typename U> friend void f(void *ptr)
I have a template class that looks something like this: template<class T> class C
My code is built to multiple .dll files, and I have a template class
Is there any way to make a template class argument optional? Specifically in this
For some reasons, I'd like to write code like this: template<class T> class C
I have a class template<size_t N, size_t M> class Matrix { // .... };

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.