m.s.
m.s.

Reputation: 16324

template which accepts either a type or a template as its parameter

Is it possible to have a template which accepts either a type or a template as its template parameter?

I tried the following using partial template specialization, but failed:

#include <type_traits>

template <template <typename...> class T>
struct TemplateHolder;

template <typename T>
struct Trait : std::true_type {};

template <template <typename...> class Template>
struct Trait<TemplateHolder<Template>> : std::true_type {};

template <typename... Ts>
struct Foo {};

struct Bar {};

static_assert(Trait<Bar>::value, "");
static_assert(Trait<Foo>::value, "");

error message from GCC:

main.cpp:18:24: error: type/value mismatch at argument 1 in template parameter list for 'template<class T> struct Trait'

 static_assert(Trait<Foo>::value, "");
                        ^
main.cpp:18:24: note:   expected a type, got 'Foo'

live example

Upvotes: 1

Views: 71

Answers (1)

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275370

static_assert(Trait<TemplateHolder<Foo>>::value, "");

Is no longer a syntax error. You must shove the templates into the holder yourself.

Other than that, no, what you ask for does not work.

Upvotes: 2

Related Questions