13

我有一个模板类 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;}

我希望班级 A<std::string,20> 做其他班级没有做的事情。如何在不更改构造函数 A:A(int) 的情况下做到这一点?

4

7 回答 7

14

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

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

如果您想A<string,20>拥有与 generic不同A的构造函数,则需要专门化整个A<string,20>类:

template<> class A<string,20> {
public:
   A(const string& takethistwentytimes) { cerr << "One Type" << std::endl; }
};
于 2009-12-14T19:46:37.650 回答
7

假设您真的打算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; }

踢轮胎:

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;
}
于 2009-12-14T19:21:33.140 回答
3

迟到但非常优雅的解决方案:C++ 2020 引入了Constraints and Concepts。您现在可以有条件地启用和禁用构造函数和析构函数!

#include <iostream>
#include <type_traits>

template<class T>
struct constructor_specialized
{
    constructor_specialized() requires(std::is_same_v<T, int>)
    {
        std::cout << "Specialized Constructor\n";
    };

    constructor_specialized()
    {
        std::cout << "Generic Constructor\n";
    };
};

int main()
{
    constructor_specialized<int> int_constructor;
    constructor_specialized<float> float_constructor;
};

在此处运行代码。

于 2020-08-20T22:06:53.920 回答
2

这可能有点晚了,但如果您有权访问,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
  }

工作示例

于 2018-01-16T14:45:41.227 回答
1

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

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

  class one_type:
    public A<std::string, 20>
  {
    one_type(int m)
      : A<str::string, 20>(m)
    {
      cerr << "One type" << endl;
    }
  };
于 2009-12-14T19:10:57.190 回答
1

怎么样 :

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
    }
};
于 2010-12-13T15:59:22.937 回答
0

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

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;}
于 2016-11-07T23:05:07.680 回答