在gcc10+中不允许重新定义,但在gcc7-9中允许。

huangapple go评论69阅读模式
英文:

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 &lt;typename R&gt;
inline constexpr bool foo = true;
template &lt;typename R&gt;
inline constexpr bool foo = false;

int main() {
  return 0;
}

In gcc10 and above, the error message is:

&lt;source&gt;:5:23: error: redefinition of &#39;template&lt;class R&gt; constexpr const bool foo&#39;
    5 | inline constexpr bool foo = false;
      |                       ^~~
&lt;source&gt;:3:23: note: &#39;template&lt;class R&gt; constexpr const bool foo&lt;R&gt;&#39; previously declared here
    3 | inline constexpr bool foo = true;
      |                       ^~~
ASM generation compiler returned: 1
&lt;source&gt;:5:23: error: redefinition of &#39;template&lt;class R&gt; constexpr const bool foo&#39;
    5 | inline constexpr bool foo = false;
      |                       ^~~
&lt;source&gt;:3:23: note: &#39;template&lt;class R&gt; constexpr const bool foo&lt;R&gt;&#39; 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.

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92576

huangapple
  • 本文由 发表于 2023年5月15日 15:40:54
  • 转载请务必保留本文链接:https://go.coder-hub.com/76251855.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定