user1899020
user1899020

Reputation: 13575

Specialize a defined type in a class template?

For example

template<class T>
struct Foo
{
    typedef int Type;
    void f();
}

Foo<T>::f() can be specialized for a specific T. How about the defined type Type? If it works, I don't need to specialize the whole class. Any ways to implement this intent?

Upvotes: 1

Views: 56

Answers (1)

Walter
Walter

Reputation: 45434

template<class T>
struct Foo
{
  typedef typename some_class_template<T>::type Type;
  void f();
};

member functions have declaration and definition and you can specialise the definition. That cannot be done for member types. These must be specialised with their declaration.

Of course, my some_class_template can be anything from the standard library, for example

  typedef typename std::conditional<sizeof(T)==4, int, T>::type Type;

Upvotes: 2

Related Questions