1

Let's say I have a template class defined as:

template < typename A, typename B >
class something { ... }

How can I test if types A and B are of the same type? I know this can be done at runtime with typeid, but I really need this to be a compile time test.

Also, How can I specialize the class if types A and B are equal?

In the real world, A would be an stl container of some sort, std::string for example, and B would be a char or wchar_t. Internally I already check the containers value_type (compile error if not what expected). If B is the same as the containers value_type, most of the code in the class will become redundant.

5
  • 1
    possible duplicate of How to check if two template parameters are exactly the same? Commented Jul 5, 2013 at 10:43
  • @hmjd Similar question, different answer. Commented Jul 5, 2013 at 10:45
  • 2
    @hmjd: There is a similar question AND a non-similar one. This question asks too much, actually. Commented Jul 5, 2013 at 10:46
  • @hmjd Thanks for the link. I hate the search feature on this site. Commented Jul 5, 2013 at 10:47
  • 1
    @Waldermort: you can also type "site:stackoverflow.com <your query>" into google. You can even make that a simple shortcut in firefox (right click on any inputbox and use "add keyword for this search"). Commented Jul 5, 2013 at 11:01

3 Answers 3

6

Also, How can I specialize the class if types A and B are equal?

By exactly that, specializing:

template <typename A>
class something<A,A> { ... }

Templates use pattern matching for their parameter lists, like also seen in many functional programming languages.

How can I test if types A and B are of the same type?

You can use std::is_same, or use specialization as above. It depends on your exact use case.

Sign up to request clarification or add additional context in comments.

Comments

4

You can check if the types are the same using:

std::is_same<A, B>::value

it will return true when they are.

Comments

1

How about this

template <typename A, typename B>
struct EnsureSameType {};

template <typename A>
struct EnsureSameType<A, A> {
    typedef int the_template_types_are_different;
};

int main()
{
    /* this should compile */
    typedef EnsureSameType<std::string::value_type, char>::the_template_types_are_different _;
    /* this should fail, and you will see the compiler
       remind you that the_template_types_are_different */
    typedef EnsureSameType<std::string::value_type, wchar_t>::the_template_types_are_different _;
    return 0;
}

6 Comments

I really like your use of non std. One less #include I need to worry about.
Seriously? Worrying about standard library includes? You need to rethink your priorities.
@Waldermort I'm not using STL solution, just because I could only get a bool value via std::is_same, but your question need a compile error.
@neuront I understand. I prefer the compile error over the stl method.
@SebastianRedl Seriously? Judging without the facts? Where strings come into play I try to stay away from STL since, in my experience, every compiler is different. For example ASCII vs UTF8 std::string.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.