18

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:

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

Martin York
  • 257,169
  • 86
  • 333
  • 562
user231536
  • 2,661
  • 4
  • 33
  • 45

7 Answers7

17

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

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

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

template<> class A<string,20> {
public:
   A(const string& takethistwentytimes) { cerr << "One Type" << std::endl; }
};
xtofl
  • 40,723
  • 12
  • 105
  • 192
11

Late but a very elegant solution: C++ 2020 introduced Constraints and Concepts. You can now conditionally enable and disable constructors and destructors!

#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;
};

Run the code here.

Marcin Poloczek
  • 923
  • 6
  • 21
7

Assuming your really meant for A::test to be publicly accessible, you could do something like this:

#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;
}
seh
  • 14,999
  • 2
  • 48
  • 58
2

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

  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

amc176
  • 1,514
  • 8
  • 19
1

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.

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

  class one_type:
    public A<std::string, 20>
  {
    one_type(int m)
      : A<str::string, 20>(m)
    {
      cerr << "One type" << endl;
    }
  };
ThomasMcLeod
  • 7,603
  • 4
  • 42
  • 80
Franci Penov
  • 74,861
  • 18
  • 132
  • 169
  • Thought so. constructor's cannot be specialized as member functions, could you provide an argument for why not ? – Hassan Syed Dec 14 '09 at 19:15
1

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
    }
};
OneOfOne
  • 95,033
  • 20
  • 184
  • 185
0

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;}
WaltK
  • 724
  • 4
  • 13