简体   繁体   中英

Out-of-class definition of member of a class template with a class-type non-type template parameter

With C++20, it's possible to define a template class taking a class-type non-type template parameter :

struct A {};

template <A a>
struct B {
    void f();
};

But is it possible to define B::f() out-of-class like with integral types? Because this

template <int>
struct C {
    void f();
};

template <int i>
void C<i>::f() {}

compiles, but this

template <A a>
void B<a>::f() {}

yields an "invalid use of incomplete type" error when I try to compile it on gcc 9. Curiously, if I replace B to take a non-type parameter of auto instead of A , it compiles just fine:

template <auto a>
struct B {
   void f();
};

template <auto a>
void B<a>::f() {}

I know support for C++20 is still experimental on gcc 9, but is this supposed to be possible or not?

Yes, the code

template <auto a>
struct B {
   void f();
};

template <auto a>
void B<a>::f() {}

will compile in C++20. Please note that the code

#include <type_traits>

template<typename T>
concept A = std::is_same<T,int>::value;

template <A a>
struct B {
   void f();
};

template <A a>
void B<a>::f() {}

will also compile in C++20, because A is a concept .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM