Papipone
Papipone

Reputation: 1123

C++ template type and type of template

What is the type of T if I write something like that:

template<typename T>
class AClass{
  private:
    T member;
  public:
    AClass(const T& value = T()) : member(value) {}
}; 

int main(){
  const char* n = "Hello";
  AClass<char*> a(n);
  return 0;
}

Does T refers to a char or a pointer over a char?

Thanks for your answers

Upvotes: 1

Views: 95

Answers (2)

skypjack
skypjack

Reputation: 50540

Facts:

Think about your constructor:

AClass(const T& value = T())

What you want is a pointer to const char, that is const char *.
In your constructor you are saying that T is const, thus you are asking for a const pointer to char, that is char * const.
They are actually two different beasts and the compiler complains about the lack of a const (let me say) in the right place in your constructor. That's because a conversion from const char * to char * is not allowed.

Upvotes: 1

sleepyVision
sleepyVision

Reputation: 11

Assuming you meant to write

AClass<char*> a('n');

T is a char* (address), but 'n' would resolve to simply char. I don't believe it would compile.

Upvotes: 0

Related Questions