英文:
Redefinition is not allowed in gcc10+ but is allowed in gcc7-9
问题
以下代码将在gcc10及以上版本中失败,但在gcc7到gcc9中通过。
我在想为什么?有人可以帮我吗?
你可以在这里尝试:https://godbolt.org/(在右侧面板中切换编译器版本)
template <typename R>
inline constexpr bool foo = true;
template <typename R>
inline constexpr bool foo = false;
int main() {
return 0;
}
在gcc10及以上版本中,错误消息如下:
<source>:5:23: error: redefinition of 'template<class R> constexpr const bool foo'
5 | inline constexpr bool foo = false;
| ^~~
<source>:3:23: note: 'template<class R> constexpr const bool foo<R>' previously declared here
3 | inline constexpr bool foo = true;
| ^~~
ASM generation compiler returned: 1
<source>:5:23: error: redefinition of 'template<class R> constexpr const bool foo'
5 | inline constexpr bool foo = false;
| ^~~
<source>:3:23: note: 'template<class R> constexpr const bool foo<R>' previously declared here
3 | inline constexpr bool foo = true;
| ^~~
Execution build compiler returned: 1
英文:
The following code will fail in gcc10 and above, but passed in gcc7 to gcc9.
I'm wondering why? Could anyone help me?
You can try here: https://godbolt.org/ (switch compiler versions in the right-side panel)
template <typename R>
inline constexpr bool foo = true;
template <typename R>
inline constexpr bool foo = false;
int main() {
return 0;
}
In gcc10 and above, the error message is:
<source>:5:23: error: redefinition of 'template<class R> constexpr const bool foo'
5 | inline constexpr bool foo = false;
| ^~~
<source>:3:23: note: 'template<class R> constexpr const bool foo<R>' previously declared here
3 | inline constexpr bool foo = true;
| ^~~
ASM generation compiler returned: 1
<source>:5:23: error: redefinition of 'template<class R> constexpr const bool foo'
5 | inline constexpr bool foo = false;
| ^~~
<source>:3:23: note: 'template<class R> constexpr const bool foo<R>' previously declared here
3 | inline constexpr bool foo = true;
| ^~~
Execution build compiler returned: 1
答案1
得分: 2
程序按照 [basic.def.odr] 是不规范的。
从 [basic.def.odr]:
> 单一定义规则
> 1) 以下各项术语为可定义项:
> * 模板实体
>
> 翻译单元不得包含对任何可定义项的多个定义。
由于变量模板是一个模板实体,翻译单元不能有多个对它的定义。
这是一个旧的 gcc 错误,后来已经修复了。
英文:
The program is ill-formed as per [basic.def.odr]
From [basic.def.odr]:
> One-definition rule
> 1) Each of the following is termed a definable item:
> * a templated entity
>
> No translation unit shall contain more than one definition of any definable item.
And since a variable template is a templated entity, the translation unit cannot have more than one definition of it.
This was an old gcc bug which has since been fixed.
答案2
得分: 1
It's a bug fixed in gcc10.
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92576
英文:
Answered by myself.
It's a bug fixed in gcc10.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论