Sergio
Sergio

Reputation: 931

typedef template class instances

I've that class hierarchy:

I would define a typedef for each instance of the template class. Each instance of the template class takes as a parameter an element of enum class. Here's the code:

#include <iostream>
using namespace std;
enum class instrument_name { none = 0, piano, guitar, trumpet, saxophone} ;
class instrument_base
{
public:
instrument_base(instrument_name name): _name(name)
{
cout << "I'm base class" << endl;
}
virtual ~instrument_base(){}
// other functions

private:
instrument_name _name;
};

template<class T>
class instrument : public instrument_base
{
public:
instrument(instrument_name name):instrument_base(name)
{
cout << "I'm template class" << endl;
}
};

typedef instrument<instrument_name::piano> Piano;    // error
typedef instrument<instrument_name::guitar> Guitar;    // error
typedef instrument<instrument_name::trumpet> Trumpet;    // error
typedef instrument<instrument_name::saxophone> Saxophone;    // error

int main()
{
cout << "Hello Instruments!" << endl;

Piano p;
Guitar g;
Trumpet tr;
Saxophone s;

return 0;
}

I'm trying to compile this example with Code::Blocks (13.12) on Windows and I get those errors: - error: type/value mismatch at argument 1 in template parameter list for
'template class instrument'| - error: expected a type, got 'piano'|

Taks for any suggestion.

Upvotes: 1

Views: 925

Answers (2)

R Sahu
R Sahu

Reputation: 206577

instrument_name::piano is not a type. Hence, you cannot use:

typedef instrument<instrument_name::piano> Piano;

You can use:

// Use a non-type template parameter.
template <instrument_name i_name>
class instrument : public instrument_base
{
   public:
      instrument():instrument_base(i_name)
   {
      cout << "I'm template class" << endl;
   }
};

typedef instrument<instrument_name::piano> Piano;
Piano p;

Upvotes: 3

Marius Bancila
Marius Bancila

Reputation: 16318

The template expects a type, not a value. Your error message explains this well, you just have to read it carefully.

You can fix this by using instrument_name instead of class in the template.

template<instrument_name T>
class instrument : public instrument_base
{
public:
   instrument() :instrument_base(T)
   {
      cout << "I'm template class" << endl;
   }
};

Upvotes: 2

Related Questions