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

led*_*kol 81 c++ gcc templates specialization

我需要为某些类型专门化模板成员函数(比如说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();
}
Run Code Online (Sandbox Code Playgroud)

这是错误消息

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

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

提前致谢.

Joh*_*itb 98

它不起作用.您需要说以下内容,但这正确

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

}
Run Code Online (Sandbox Code Playgroud)

明确专门的成员也需要明确专门化他们周围的类模板.所以你需要说以下内容,这只会使成员专门化X<int>.

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

}
Run Code Online (Sandbox Code Playgroud)

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

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>) {

   }
};
Run Code Online (Sandbox Code Playgroud)

  • @Nim对,我认为指针转换的东西是丑陋的,并且不能用于你不能形成指针(引用)的类型.此外,如果函数参数是指向没有大小的数组类型的指针,则在C++中是非法的.将它放在类型包装器中使它适用于所有类型. (3认同)
  • @ JohannesSchaub-litb:重载的其他选择是什么?可以展示一些吗? (2认同)
  • @ fast-reflexes他的评论是使用`template <typename T> void get_as(T*); void get_as(double*);`并传递一个`(T*)0`. (2认同)

2b-*_*b-t 23

在C++17及以后的版本中,最简洁的方法可能是结合使用 aif constexprstd::is_same_v类型特征,而无需显式专门化:

#include <iostream>
#include <type_traits>

template <typename C>
class X {
  public:
    template <typename T> 
    void get_as() { 
      // Implementation part for all types
      std::cout << "get as ";

      // Implementation part for each type separately
      if constexpr (std::is_same_v<double, T>) {
        std::cout << "'double'";
      } else if constexpr (std::is_same_v<int, T>) {
        std::cout << "'int'";
      } else {
        std::cout << "(default)";
      }

      // Implementation part for all types
      std::cout << std::endl;
      return;
    }
};

int main() {
  X<int> d {};
  d.get_as<double>(); // 'double'
  d.get_as<int>();    // 'int'
  d.get_as<float>();  // (default)

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

在这里尝试一下!


如果您还需要返回类型,则可以将返回类型声明为auto

template <typename T> 
auto get_as() { 
  if constexpr (std::is_same_v<double, T>) {
    return 0.5;
  } else {
    return 0;
  }
}
Run Code Online (Sandbox Code Playgroud)

  • 它非常优雅,并且允许两个专业有共同的部分。谢谢! (2认同)

Gab*_*iel 20

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

这将是这样的:

#include <iostream>
#include <type_traits>

template <class C> class X
{
public:
    template <class T, typename std::enable_if< ! std::is_same<double,T>::value>::type * = nullptr > void get_as(){
        std::cout << "get as T" << std::endl;
    }


    template <class T, typename std::enable_if< std::is_same<double,T>::value>::type * = nullptr  > void get_as(){
        std::cout << "get as double" << std::endl;
    }
};


int main()
{

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

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

丑陋的是,所有这些enable_if只需要为编译器提供一个专门化,否则会出现消歧错误.这就是为什么默认行为"get as T"也需要启用if.