模板类参数类型的模板类成员的特化

发布于 2024-08-31 23:19:04 字数 546 浏览 6 评论 0原文

我有一个模板化的类矩阵。我想为复杂类型专门化一个函数,其中 T 可以是任何东西。我已经尝试过:

  6 template <typename T>
  7 class Matrix {
  8       public :
  9             static void f();
 10 };          
 11 template<typename T> void Matrix<T>::f() { cout << "generic" << endl; }
 12 template<> void Matrix<double>::f() { cout << "double" << endl; }
 13 template<typename T> void Matrix<std::complex<T> >::f() { cout << "complex" << endl; }

第 13 行无法编译。我怎样才能做到这一点?

I have a templated class Matrix. I want to specialize a function for the type complex, where T can be anything. I have tried this :

  6 template <typename T>
  7 class Matrix {
  8       public :
  9             static void f();
 10 };          
 11 template<typename T> void Matrix<T>::f() { cout << "generic" << endl; }
 12 template<> void Matrix<double>::f() { cout << "double" << endl; }
 13 template<typename T> void Matrix<std::complex<T> >::f() { cout << "complex" << endl; }

Line 13 does not compile. How can I do that ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

2024-09-07 23:19:04

在第 11 行和第 12 行中,您对类模板的成员进行了显式特化声明,这是 C++ 标准 14.7/3 所允许的(14.5.2/2 也包含一个很好的示例)。在第 13 行中,您尝试部分特化一个类模板,但这种形式不允许这样做(这是部分特化,因为您不知道整个类型 std::complex 因为它仍然取决于 T)。你应该对整个班级进行部分专业化。

In lines 11 and 12 you have declaration of explicit specialization for a member of a class template which is allowed by C++ Standard 14.7/3 (14.5.2/2 contains a good example too). In line 13 you are trying to partially specialize a class template and that is not allowed in this form (this is partial specialization because you don't know the whole type std::complex<T> because it is still depends on T). You should partially specialize the whole class.

撧情箌佬 2024-09-07 23:19:04

事实上,我通过 Boost 找到了一个巧妙的方法。由于我不希望我的库依赖于 Boost,因此代码如下:

template <class T, T val> struct integral_constant
{
      typedef integral_constant<T, val> type;
      typedef T value_type;
      static const T value = val;
};    
typedef integral_constant<bool, true>  true_type;
typedef integral_constant<bool, false> false_type;
template <typename T> struct is_complex : false_type{};
template <typename T> struct is_complex<std::complex<T> > : true_type{};

template <typename T>
class Matrix {
      public :
            static void f() { f_( typename is_complex<T>::type() ); }
      private :
            static void f_( true_type ) { cout << "generic complex" << endl; }
            static void f_( false_type ) { cout << "generic real" << endl; }
};          
template<> void Matrix<double>::f() { cout << "double" << endl; }

这样,我可以使用函数重载和模板来实现我的目标。

In fact, I found a clever way to do it through Boost. Since I don't want my library to be dependant on Boost, here is the code :

template <class T, T val> struct integral_constant
{
      typedef integral_constant<T, val> type;
      typedef T value_type;
      static const T value = val;
};    
typedef integral_constant<bool, true>  true_type;
typedef integral_constant<bool, false> false_type;
template <typename T> struct is_complex : false_type{};
template <typename T> struct is_complex<std::complex<T> > : true_type{};

template <typename T>
class Matrix {
      public :
            static void f() { f_( typename is_complex<T>::type() ); }
      private :
            static void f_( true_type ) { cout << "generic complex" << endl; }
            static void f_( false_type ) { cout << "generic real" << endl; }
};          
template<> void Matrix<double>::f() { cout << "double" << endl; }

This way, I can use function overloading and template to achievement my goal.

坏尐絯℡ 2024-09-07 23:19:04

正如链接答案中所述,您需要做的是专门化整个类,而不是简单的函数:

#include <iostream>
#include <complex>
using namespace std;

template <typename T>
class Matrix {
public :
    static void f();
};

template<typename T> void Matrix<T>::f() { cout << "generic" << endl; }
template<> void Matrix<double>::f() { cout << "double" << endl; }

template <typename T>
class Matrix<std::complex<T> > {
public:
    static void f() { cout << "complex" << endl; }
};

int main(void) {
  Matrix<complex<double> >::f();
  return 0;
}

As describe in the linked answer, what you'll need to do is specialize the entire class, rather than the simple function:

#include <iostream>
#include <complex>
using namespace std;

template <typename T>
class Matrix {
public :
    static void f();
};

template<typename T> void Matrix<T>::f() { cout << "generic" << endl; }
template<> void Matrix<double>::f() { cout << "double" << endl; }

template <typename T>
class Matrix<std::complex<T> > {
public:
    static void f() { cout << "complex" << endl; }
};

int main(void) {
  Matrix<complex<double> >::f();
  return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文