简体   繁体   English

默认模板参数的非冲突重新定义

[英]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:因此,我正在尝试使用 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'错误:重新定义“class T”的默认参数

Where is my understanding incorrect?我的理解哪里不对?

You cannot redefine default template parameters.您不能重新定义默认模板参数。

When you write template <class T = int> class A {};当你写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).您必须选择一个位置来放置该默认参数(前向声明或实际定义)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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