如何在 C++ 中的类体之外定义一个专门的类方法?

Fed*_*dor 6 c++ partial-specialization c++-concepts c++20

我有一个模板类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(); }
Run Code Online (Sandbox Code Playgroud)

GCC 接受此代码。

Clang 打印错误https://gcc.godbolt.org/z/hYfYGPfMh

error: type constraint differs in template redeclaration
template<std::integral T>
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

请建议如何在类体之外定义方法并使所有编译器满意?

dot*_*icu 2

我非常确定 MS 和 Clang 编译器在这里都出错,并且 GCC 正在正确编译您的代码。在其他编译器中修复这些错误之前,我建议继续使用概念模式,而不是回到过时的方法。只需使用附加类来解决该错误:

#include <concepts>
#include <iostream>

// This is a work-around for using concept specialization of
// classes in conjunction with out-of-body definition of members.
// Only helpful for MSVC and Clang. GCC is properly compiling
// out-of-body concept specializations.

template <typename T>
class A
{
    // For MSVC ONLY: the default template seems require being empty
    // for this to work, but do fiddle around with it.
    
    // (Also works with MSVC:)
    A()                         = delete;
    A(const A&)                 = delete;
    A(A&&) noexcept             = delete;
    A& operator =(const A&)     = delete;
    A& operator =(A&&) noexcept = delete;
    ~A()                        = delete;

    // Clang and GCC can have members just fine:
    // static const char* foo();
};

// We use distinct base classes to define our concept specializations of A.
template <std::signed_integral T>
class A_Signed_Integral
{
public:
    static const char* foo();
};
template <std::unsigned_integral T>
class A_Unsigned_Integral
{
public:
    static const char* foo();
};

// And then we wrap them using the actual concept specializations of A,
// making the specializations effectivel the same class as the base class.
template <std::signed_integral T>
class A<T> :
    public A_Signed_Integral<T>
{
public:
    using A_Signed_Integral<T>::A_Signed_Integral;  // grab all ctors
    using A_Signed_Integral<T>::operator =;         // an exceptional case
};
template <std::unsigned_integral T>
class A<T> :
    public A_Unsigned_Integral<T>
{
public:
    using A_Unsigned_Integral<T>::A_Unsigned_Integral;
    using A_Unsigned_Integral<T>::operator =;
};

// Out-of-body definitions can be located to another file
template <std::signed_integral T>
inline const char* A_Signed_Integral<T>::foo()
{
    return "using A<std::signed_integral T> foo";
}

template <std::unsigned_integral T>
inline const char* A_Unsigned_Integral<T>::foo()
{
    return "using A<std::unsigned_integral T> foo";
}

int main()
{
    std::cout << A<signed long>::foo() << std::endl;
    std::cout << A<unsigned long>::foo() << std::endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

(使用所有三个编译器进行测试并且工作正常:请参阅 gcc.godbolt.org

将来,一旦错误得到修复,通过搜索和替换来删除基类应该相对容易,而只使用 A 的概念专业化。

编辑:更新示例以适用于 MSVC,它似乎还无法使用默认模板。