构造函数的 C++ 模板特化

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1902827/
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-27 21:32:30  来源:igfitidea点击:

C++ template specialization of constructor

c++templatesconstructorspecialization

提问by user231536

I have a templated class A<T, int> and two typedefs A<string, 20> and A<string, 30>. How do I override the constructor for A<string, 20> ? The following does not work:

我有一个模板类 A<T, int> 和两个 typedef A<string, 20> 和 A<string, 30>。如何覆盖 A<string, 20> 的构造函数?以下不起作用:

template <typename T, int M> class A;
typedef  A<std::string, 20> one_type;
typedef  A<std::string, 30> second_type;


template <typename T, int M>
class A {
public:
  A(int m) {test= (m>M);}

  bool test;

};


template<>
one_type::one_type() { cerr << "One type" << endl;}

I would like the class A<std::string,20> to do something that the other class doesn't. How can I do this without changing the constructor A:A(int) ?

我希望 A<std::string,20> 类做一些其他类没有做的事情。如何在不更改构造函数 A:A(int) 的情况下执行此操作?

回答by xtofl

The only thing you cannot do is use the typedefto define the constructor. Other than that, you ought to specialize the A<string,20>constructor like this:

您唯一不能做的就是使用typedef来定义构造函数。除此之外,您应该A<string,20>像这样专门化构造函数:

template<> A<string,20>::A(int){}

If you want A<string,20>to have a differentconstructor than the generic A, you need to specialize the whole A<string,20>class:

如果你想A<string,20>有一个不同于generic 的构造函数A,你需要专门化整个A<string,20>类:

template<> class A<string,20> {
public:
   A(const string& takethistwentytimes) { cerr << "One Type" << std::endl; }
};

回答by seh

Assuming your reallymeant for A::testto be publicly accessible, you could do something like this:

假设您真的打算A::test公开访问,您可以执行以下操作:

#include <iostream>


template <int M>
struct ABase
{
  ABase(int n) : test_( n > M )
  {}

  bool const test_;
};


template <typename T, int M>
struct A : ABase<M>
{
  A(int n) : ABase<M>(n)
  {}
};


template <typename T>
A<T, 20>::A(int n)
  : ABase<20>(n)
  { std::cerr << "One type" << std::endl; }

Kick the tires:

踢轮胎:

int main(int argc, char* argv[])
{
  A<int, 20> a(19);
  std::cout << "a:" << a.test_ << std::endl;
  A<int, 30> b(31);
  std::cout << "b:" << b.test_ << std::endl;
  return 0;
}

回答by amc176

This may be a little bit late, but if you have access to c++11you can use SFINAEto accomplish just what you want:

这可能有点晚了,但是如果您可以访问,则c++11可以使用SFINAE来完成您想要的操作:

  template <class = typename std::enable_if< 
    std::is_same<A<T,M>, A<std::string, 20>>::value>::type // Can be called only on A<std::string, 20>
  > 
  A() {
    // Default constructor
  }

Working example

工作示例

回答by Franci Penov

You can't with your current approach. one_type is an alias to a particular template specialization, so it gets whatever code the template has.

你不能用你目前的方法。one_type 是特定模板特化的别名,因此它获取模板具有的任何代码。

If you want to add code specific to one_type, you have to declare it as a subclass of A specialization, like this:

如果要添加特定于 one_type 的代码,则必须将其声明为 A 特化的子类,如下所示:

  class one_type:
    public A<std::string, 20>
  {
    one_type(int m)
      : A<str::string, 20>(m)
    {
      cerr << "One type" << endl;
    }
  };

回答by OneOfOne

How about :

怎么样 :

template<typename T, int M, bool dummy = (M > 20) >
class A {
public:
  A(int m){
      // this is true
  }

};

template<typename T, int M>
class A<T,M,false> {
public:
    A(int m) {
    //something else
    }
};

回答by WaltK

The best solution I've been able to come up with for this situation is to use a "constructor helper function":

对于这种情况,我能够提出的最佳解决方案是使用“构造函数辅助函数”:

template <typename T, int M> class A;
typedef  A<std::string, 20> one_type;
typedef  A<std::string, 30> second_type;

template <typename T, int M>
class A {
private:
  void cons_helper(int m) {test= (m>M);}
public:
  A(int m) { cons_helper(m); }

  bool test;
};

template <>
void one_type::cons_helper(int) { cerr << "One type" << endl;}