简体   繁体   English

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

[英]How to define a specialized class method outside of class body in C++?

I have a template class A<T> and its specialization for integral arguments.我有一个模板类A<T>及其对整数参数的专业化。 And both the class and its specialization declare the method foo() , which I would like to define outside of class bodies:并且类及其特化都声明了方法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 accepts this code. GCC 接受此代码。

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

error: type constraint differs in template redeclaration
template<std::integral T>

And MSVC prints errors on both method definitions: 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

Please suggest how to define methods outside class bodies and make all the compilers happy?请建议如何在类体之外定义方法并使所有编译器满意?

I am not a C++ template expert, I tried something like below我不是 C++ 模板专家,我尝试过类似下面的方法

template<class T, bool = std::is_integral_v<T>>
struct A
{};

template<class T>
struct A<T, false>
{ 
   static void foo(); 
};

template<class T>
struct A<T, true> 
{ 
   static void foo(); 
};

template<class T>
void A<T,false>::foo() 
{
  std::cout << "I am working on a non-integral type" << std::endl;
}

template<class T>
void A<T, true>::foo() 
{
  std::cout << "I am working on an integral type" << std::endl;
}

int main()
{
  A<int>::foo();
  A<float> ::foo();

  return 0;
}

and the code gave me the results on MS C++ compiler代码给了我 MS C++ 编译器的结果

I am working on an integral type
I am working on a non-integral type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在声明之外定义一个专用于无参数的可变参数模板类的C ++方法? - how to define a C++ method outside the declaration for a variadic templated class specialized to no parameters? 模板类,使用一种专门用于C ++的方法 - template class with a single method specialized in C++ C ++:如何在声明它的模板类主体之外定义一个枚举类? - C++: How to define an enum class outside of a template class body in which it is declared? 如何在类主体之外定义类模板的模板化方法 - How to define a templated method of a class template outside of class body 如何在 C++ 中定义其父级之外的嵌套类 - How to define a nested class outside its parent in C++ 如何为(某种程度上)部分专用的类定义函数 - How to define a function for a (somewhat) partially specialized class 如何在完全专门的模板类的定义之外定义模板成员函数? - How do I define a template member function outside of a full specialized template class's definition? 专业模板类的朋友(C ++) - friend of specialized template class (C++) c ++从专门的模板类继承 - c++ inherit from specialized template class 如何在类外部定义Lambda函数并在C ++中的类内部使用它? - How to define a lambda function outside of a class and use it inside a class in C++?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM