C++ 模板类成员函数的显式特化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5512910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 18:20:10  来源:igfitidea点击:

explicit specialization of template class member function

c++templatesgccspecialization

提问by ledokol

I need to specialize template member function for some type (let's say double). It works fine while class Xitself is not a template class, but when I make it template GCC starts giving compile-time errors.

我需要为某种类型专门化模板成员函数(比如说double)。虽然类X本身不是模板类,但它工作正常,但是当我将其设为模板时,GCC 开始给出编译时错误。

#include <iostream>
#include <cmath>

template <class C> class X
{
public:
   template <class T> void get_as();
};

template <class C>
void X<C>::get_as<double>()
{

}

int main()
{
   X<int> x;
   x.get_as();
}

here is the error message

这是错误信息

source.cpp:11:27: error: template-id
  'get_as<double>' in declaration of primary template
source.cpp:11:6: error: prototype for
  'void X<C>::get_as()' does not match any in class 'X<C>'
source.cpp:7:35: error: candidate is:
  template<class C> template<class T> void X::get_as()

How can I fix that and what is the problem here?

我该如何解决这个问题,这里有什么问题?

Thanks in advance.

提前致谢。

回答by Johannes Schaub - litb

It doesn't work that way. You would need to say the following, but it is notcorrect

它不会那样工作。你需要说以下,但它是正确的

template <class C> template<>
void X<C>::get_as<double>()
{

}

Explicitly specialized membersneed their surrounding class templates to be explicitly specialized as well. So you need to say the following, which would only specialize the member for X<int>.

显式专门化的成员也需要显式专门化它们周围的类模板。因此,您需要说出以下内容,这只会专门针对X<int>.

template <> template<>
void X<int>::get_as<double>()
{

}

If you want to keep the surrounding template unspecialized, you have several choices. I prefer overloads

如果您想让周围的模板保持非专业化,您有多种选择。我更喜欢重载

template <class C> class X
{
   template<typename T> struct type { };

public:
   template <class T> void get_as() {
     get_as(type<T>());
   }

private:
   template<typename T> void get_as(type<T>) {

   }

   void get_as(type<double>) {

   }
};

回答by Gabriel

If one is able to used std::enable_ifwe could rely on SFINAE (substitution failure is not an error)

如果可以使用,std::enable_if我们可以依靠 SFINAE(替换失败不是错误)

that would work like so (see LIVE):

这会像这样工作(请参阅LIVE):

#include <iostream>
#include <type_traits>

template <typename C> class X
{
public:
    template <typename T, 
              std::enable_if_t<!std::is_same_v<double,T>, int> = 0> 
    void get_as() { std::cout << "get as T" << std::endl; }

    template <typename T, 
              std::enable_if_t<std::is_same_v<double,T>, int> = 0> 
    void get_as() { std::cout << "get as double" << std::endl; }
};

int main() {
   X<int> d;
   d.get_as<double>();

   return 0;
}

The ugly thing is that, with all these enable_if's only one specialization needs to be available for the compiler otherwise disambiguation error will arise. Thats why the default behaviour "get as T" needs also an enable if.

丑陋的是,所有这些 enable_if 只需要一个特化可用于编译器,否则会出现消歧错误。这就是为什么默认行为“get as T”也需要启用 if。