admin管理员组文章数量:1355597
When repeating the declaration of a template class with defaulted argument, I get an error:
<source>:12:23: error: redefinition of default argument for 'class<template-parameter-1-2>'
12 | template <typename T, typename = T>
| ^~~~~~~~
<source>:9:23: note: original definition appeared here
9 | template <typename T, typename = T>
|
Repeating a template forward declaration without default arguments doesn't give any errors. Repeating the same one, but without defaulting the argument also works (but IMO it doesn't make sense and should give some error).
Here's the whole example:
template <typename T>
struct ok;
template <typename T>
struct ok;
template <typename T, typename = T>
struct not_ok;
template <typename T, typename = T>
struct not_ok;
template <typename T, typename = T>
struct why_is_it_ok;
template <typename T, typename>
struct why_is_it_ok;
WHY?
When repeating the declaration of a template class with defaulted argument, I get an error:
<source>:12:23: error: redefinition of default argument for 'class<template-parameter-1-2>'
12 | template <typename T, typename = T>
| ^~~~~~~~
<source>:9:23: note: original definition appeared here
9 | template <typename T, typename = T>
|
Repeating a template forward declaration without default arguments doesn't give any errors. Repeating the same one, but without defaulting the argument also works (but IMO it doesn't make sense and should give some error).
Here's the whole example:
template <typename T>
struct ok;
template <typename T>
struct ok;
template <typename T, typename = T>
struct not_ok;
template <typename T, typename = T>
struct not_ok;
template <typename T, typename = T>
struct why_is_it_ok;
template <typename T, typename>
struct why_is_it_ok;
WHY?
Share Improve this question asked Mar 31 at 12:36 Sergey KolesnikSergey Kolesnik 3,7272 gold badges14 silver badges44 bronze badges 5 |1 Answer
Reset to default 6Just like default parameters for function parameters, default parameters for template parameters are only allowed to be set once per scope.
template <typename T>
struct ok;
template <typename T>
struct ok;
Is fine because neither declaration introduces a default parameter.
template <typename T, typename = T>
struct not_ok;
template <typename T, typename = T>
struct not_ok;
Is not allowed because the second unnamed parameter is defaulted twice in the same scope.
template <typename T, typename = T>
struct why_is_it_ok;
template <typename T, typename>
struct why_is_it_ok;
Is allowed because only the first declaration declares a default parameter
本文标签: cError when repeating forward declaration with default template argumentStack Overflow
版权声明:本文标题:c++ - Error when repeating forward declaration with default template argument - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743947453a2566667.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
template <typename T, typename = int> struct not_ok;
. – Marek R Commented Mar 31 at 13:08template <typename T, typename = X> struct not_ok;
, there could be a differentX
visible at different points. In your specific case that is not possible, but there is no exception for the types being the same. You can only give a default value once. – BoP Commented Mar 31 at 13:18