无法找到 gcc13.2 发行说明中的​​更改或错误:具有不同约束的 template<typename> 的无效重新声明

Mrm*_*kip 1 c++ gcc compiler-bug c++20

几天前,我在 arch linux 上使用arm-none-eabi-gcc工具链时遇到了以下问题,它使用gcc13.2作为gcc版本,与我在Ubuntu中使用的工具链不同,我认为它会是某个版本所以我去了 gcc 发布页面阅读注释:https://gcc.gnu.org/gcc-13/changes.html,但是正如您所见,没有提到任何有关模板重新声明的内容

经过一些研究,我得出了这个可重现的例子:

// compiles fine under gcc 12.3

#include <concepts>
#include <type_traits>

 template<typename T>
 class Sensor{
   public:
    int read();
  };

template<typename T>
  requires std::is_integral_v<T>
int Sensor<T>::read(){
 return 1;
}

int main(){
 Sensor<int>  s;
 s.read();
}


//does not compile under gcc 13.2
#include <concepts>
#include <type_traits>

template<typename T>

class Sensor{
 public:
    int read();
};

template<typename T>
 requires std::is_integral_v<T>
int Sensor<T>::read(){
 return 1;
}

int main(){
 Sensor<int>  s;
 s.read();
}
Run Code Online (Sandbox Code Playgroud)

有人可以解释为什么在 12.3 版本中允许,但在 13.2 中不允许吗?另外我认为 13.2 中强制执行的约束应该是事实上的标准

Ted*_*gmo 5

相关注释位于gcc/cp/ChangeLog

2023-03-14  Patrick Palka  <...>

        PR c++/96830
        * pt.cc (push_inline_template_parms_recursive): Set
        TEMPLATE_PARMS_CONSTRAINTS.
        (push_template_decl): For an out-of-line declaration, verify
        constraints for each enclosing template scope match those of the
        original template declaratation.


    gcc/testsuite/ChangeLog:

            * g++.dg/cpp2a/concepts-class5.C: New test.
            * g++.dg/cpp2a/concepts-class5a.C: New test.
Run Code Online (Sandbox Code Playgroud)

有人可以解释为什么在 12.3 版本中允许,但在 13.2 中不允许吗?

12.3 是在提交此错误修复之前发布的。


完整的提交消息是:

commit cd5baeb4489b6a953abbc7f02fea457fd9ed2f83
Author: Patrick Palka <...>
Date:   Tue Mar 14 19:14:29 2023 -0400

    c++: redeclaring member of constrained class template [PR96830]

    An out-of-line definition of a member of a constrained class template
    needs to repeat the template's constraints, but it turns out we don't
    verify anywhere that the two sets of constraints match.  This patch
    adds such a check to push_template_decl, nearby a similar consistency
    check for the template parameter list lengths.

            PR c++/96830

    gcc/cp/ChangeLog:

            * pt.cc (push_inline_template_parms_recursive): Set
            TEMPLATE_PARMS_CONSTRAINTS.
            (push_template_decl): For an out-of-line declaration, verify
            constraints for each enclosing template scope match those of the
            original template declaratation.

    gcc/testsuite/ChangeLog:

            * g++.dg/cpp2a/concepts-class5.C: New test.
            * g++.dg/cpp2a/concepts-class5a.C: New test.
Run Code Online (Sandbox Code Playgroud)

  • @Mrmudkip C++ 很棘手。所有编译器都有错误:-) (3认同)