【发布时间】:2021-07-30 10:20:02
【问题描述】:
我有一个模板类A<T> 及其对整数参数的专门化。并且类及其特化都声明了方法foo(),我想在类主体之外定义它:
#include <concepts>
template<class T>
struct A { static void foo(); };
template<std::integral T>
struct A<T> { static void foo(); };
template<class T>
void A<T>::foo() {}
template<std::integral T>
void A<T>::foo() {}
int main() { A<int>::foo(); }
GCC 接受此代码。
Clang 打印错误 https://gcc.godbolt.org/z/hYfYGPfMh :
error: type constraint differs in template redeclaration
template<std::integral T>
MSVC 会在两个方法定义上打印错误:
error C3855: 'A<T>': template parameter 'T' is incompatible with the declaration
error C2447: '{': missing function header (old-style formal list?)
error C2065: 'foo': undeclared identifier
请建议如何在类体之外定义方法并使所有编译器满意?
【问题讨论】:
-
好像是gcc的bug。 see marked answer in other question.
-
我真诚地希望 gcc 是其中正确的一个,其余的得到修复,否则我看不出应该如何调整用户代码。不过,我不确定章节和诗句。无论如何,把它变成一个language-lawyer-question 并附带一个可行的解决方法?
-
@ГеоргийГуминов 另一个问题是关于 requires 子句,但这里没有
-
@ГеоргийГуминов 可能相关,但明显不同。可能偶然表明 gcc 在这种情况下具有更有用(并且希望是正确的)行为。
-
@ГеоргийГуминов 实际上这里的情况更类似于第二个答案中的“ok”
标签: c++ c++20 c++-concepts partial-specialization