I had the following issue a couple of days ago when using arm-none-eabi-gcc toolchain on arch linux, which uses gcc13.2 as the gcc version, unlike the toolchain I was using in Ubuntu, I assumed it would be some version stuff so I went to the gcc release page to read the notes: https://gcc.gnu.org/gcc-13/changes.html, but as you will see nothing about redeclaration of templates is mentioned
upon some research I came to this reproducible example:
// 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();
}
Could someone explain why in version 12.3 it is allowed but not in 13.2? Also I'd assume the constrainst enforced in 13.2 should have been a standard de facto way ago
The relevant note is in 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.
Could someone explain why in version 12.3 it is allowed but not in 13.2?
12.3 was released before this bugfix was submitted.
The full commit message is:
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.