Click here to Skip to main content
11,479,170 members (63,026 online)
Click here to Skip to main content

Tagged as

Quickly Check whether C++ Template Instances have the Same Parameters

, 7 Feb 2012 CPOL 3.6K 1
Rate this:
Please Sign up or sign in to vote.
How to quickly check whether C++ Template Instances have the same parameters

Note: From Sebastian's tip, it does not seem clear to me if only the template parameter type should be checked or also its value. He might want to check type and value without raising compiler errors. My alternative only deals with the template parameter types.

If RTTI is not present, you can still use the typeid keyword. My contribution to this topic is then as follows:

#include <iostream>

template<class T>
class my_template
{
public:
    template<class T2>
    bool type_equals(const T2& o) const
    {
        return typeid(*this) == typeid(o);
    }
};
 
int _tmain(int argc, _TCHAR* argv[])
{
    my_template<int> a;
    my_template<double> b;
    my_template<int> c;
 
    std::cout << std::boolalpha;
    std::cout << "Type of a equals a: " << a.type_equals(b) << std::endl;
    std::cout << "Type of a equals c: " << a.type_equals(c) << std::endl;
 
    char ch;
    std::cin >> ch;
    return 0;
}

For the fun of it, you can also use the std::is_same template:

#include <iostream>

template<class T>
class my_template
{
public:
    typedef T parameter_type;
};

template<class T1, class T2> 
bool has_same_parameter_type(const my_template<T1>& o1, const my_template<T2>& o2)
{
    return std::is_same<T1, T2>::value;
}

int _tmain(int argc, _TCHAR* argv[])
{
    my_template<int> a;
    my_template<double> b;
    my_template<int> c;
    std::string d;        
 
    std::cout << std::boolalpha;
    std::cout << "Type of a equals a: " << has_same_parameter_type(a, b) << std::endl;
    std::cout << "Type of a equals c: " << has_same_parameter_type(a, c) << std::endl;
 
    // This would raise a compiler error:
    // std::cout << "Type of a equals d: " << has_same_parameter_type(a, d)  << std::endl;

    char ch;
    std::cin >> ch;
    return 0;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Doc Lobster
Software Developer
Germany Germany
No Biography provided

Comments and Discussions

 
GeneralThis only works if the base class (which is missing in your ... Pin
Sebastian Krysmanski7-Feb-12 22:12
memberSebastian Krysmanski7-Feb-12 22:12 
GeneralRe: Maybe its a misunderstanding and we are not trying to do the... Pin
Doc Lobster8-Feb-12 1:45
memberDoc Lobster8-Feb-12 1:45 
GeneralRe: I see the point now. You want to do the comparison through t... Pin
Doc Lobster9-Feb-12 4:04
memberDoc Lobster9-Feb-12 4:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 7 Feb 2012
Article Copyright 2012 by Doc Lobster
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid