简体   繁体   中英

Non-conflicting redefinition of default template argument

I understand ( here is one source) that one can re-define default template arguments as long as the two definitions are not in conflict. So, I am trying to compile the following with g++ 5.3.1:

template <class = int> class A; // forward declaration
template <class T = A<>> struct B {};

template <class T = int> class A {}; // "= int" here is for clarity

int main() { return 0; }

The compiler complains:

error: redefinition of default argument for 'class T'

Where is my understanding incorrect?

You cannot redefine default template parameters.

When you write template <class T = int> class A {}; you are redefining this default parameter and that is why you get the error. You must choose one place to put that default parameter (either the forward declaration or the actual definition).

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