2

Consider this simple, but complete example:

#include <iostream>

enum class Foo
{
    A,
    B
};

template <Foo F>
struct X{};

template <Foo F>
constexpr Foo deduce_foo(X<F>&& arg)
{
    return F;
}

int main()
{
    if ( deduce_foo(X<Foo::B>{}) == Foo::A )
        std::cout << "A";
    else
        std::cout << "B";
    std::cout << std::endl;
}

which deduces the enum class correctly (function deduce_foo(...)).

Now, I want to make it more generic, substituting X by a template T, i.e:

template <Foo F, typename T>
constexpr Foo deduce_foo(T<F>&& arg) { ... }

But compiler (g++ 7.3.0) gives me this error:

error: ‘T’ is not a template constexpr Foo deduce_foo(X&& arg)

What Im doing wrong?

Amadeus
  • 10,199
  • 3
  • 25
  • 31

1 Answers1

3

you need to declare that T is a template, try:

template <Foo F, template<Foo> class T>
constexpr Foo deduce_foo(T<F>&& arg)
{
    return F;
}
Alan Birtles
  • 32,622
  • 4
  • 31
  • 60