This issue occurs when trying to split the template into declaration and definition for ease of reading.

Consider this MWE

#include <type_traits>

template <typename T1, typename T2, int N = 6>
struct Foo {
  template <int N2 = N, typename std::enable_if<N2 == 6, bool>::type = true>
  Foo(int i);

  template <int N2 = N, typename std::enable_if<N2 != 6, bool>::type = true>
  Foo(double d);

};

template <typename T1, typename T2, int N>
template <int N2, typename std::enable_if<N == 6, bool>::type >
Foo<T1, T2, N>::Foo(int i){}

template <typename T1, typename T2, int N>
template <int N2, typename std::enable_if<N != 6, bool>::type >
Foo<T1, T2, N>::Foo(double d){}


int main(int argc, char** argv) {
    Foo<int, double, 3> foo1(4.5);
    Foo<int, double, 6> foo2(4);
    return 0; 
}

I want to have the class template have one constructor when N == 6 and another one if not. The above code works when putting the constructor body with the declaration, but I can't seem to find the right syntax when splitting them.

The compiler error from Godbolt is

<source>:15:17: error: out-of-line definition of 'Foo<T1, T2, N>' does not match any declaration in 'Foo<T1, T2, N>'
Foo<T1, T2, N>::Foo(int i){}
                ^~~
<source>:19:17: error: out-of-line definition of 'Foo<T1, T2, N>' does not match any declaration in 'Foo<T1, T2, N>'
Foo<T1, T2, N>::Foo(double d){}
                ^~~
2 errors generated.
Compiler returned: 1

What is my mistake here?


I can use C++20 if needed, but solutions for 17 or 14 would be preferable.

0

There are 0 best solutions below