ChatGPT解决这个技术问题 Extra ChatGPT

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

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

这是错误消息

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

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

提前致谢。

这在当前标准中是非法的,要专业化,您还必须专业化课程......
但如果类不是模板,它就可以工作。也违法吗?
不,那很好,仅适用于该规则适用的类模板(AFAIK)。

n
nonsensickle

它不是那样工作的。您需要说以下内容,但这是不正确的

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

}

明确地 specialized members 也需要明确地专门化其周围的类模板。因此,您需要说出以下内容,这只会将成员专门化为 X<int>

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

}

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

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

   }
};

为什么需要 type<> 包装器?不能将 0 强制转换为 T 类型的指针吗?估计没那么优雅...
看来这实在是做不到。谢谢。
@Nim 是的,我认为指针转换很丑陋,并且不适用于您无法形成指向(引用)的指针的类型。此外,将函数参数作为指向没有大小的数组类型的指针在 C++ 中是非法的。将它放在类型包装器中使其适用于所有类型。
@JohannesSchaub-litb:重载的其他选择是什么?可以展示其中的一些吗?
@fast-reflexes 他的评论是使用 template<typename T> void get_as(T*); void get_as(double*); 并传递 (T*)0
G
Gabriel

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

会这样工作(见 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;
}

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


更新了帖子以使其更现代。
2
2b-t

C++17 及以后的版本中,最简洁的方法可能是将 if 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;
}

Try it here!

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

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

它非常优雅,允许有两个专业的通用部分。谢谢!