-1

When defining a template, the format is:

template <class T> returnType templateName{...};

My question is: will the class keyword inside the above template declaration makes data type T a class? The question is explained below:

inside <class T>, T should be a name of data type, and it's preceded by "class". When learning class in cpp, I know that a class is defined:class ClassName{...};. So my understanding is that everything followed by a class keyword would be the name of a class. In the case of the template declaration, there is also a class keyword before T. Is this means the data types in CPP are also classes?

Psyduck
  • 637
  • 3
  • 10
  • 22
  • 5
    Whoever told you that 'everything in CPP can be a object/class' was lying through their teeth. I suggest that instead of listening to this rubbish, you pick up a good book: https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – SergeyA Aug 15 '18 at 20:45
  • 3
    In a template parameter declaration, `class` and `typename` are identical and interchangeable, so `` and `` mean the same thing, it is up to programmer preference which one to use when writing your own templates. – Remy Lebeau Aug 15 '18 at 20:46
  • An instance of any thing, including classes, is an object in C++. – juanchopanza Aug 15 '18 at 20:47
  • @SergeyA Well, mostly they are correct. Piratically everything in C++ is an object. – NathanOliver Aug 15 '18 at 20:47
  • @NathanOliver but not everything in C++ is a class! Also, not everything is an object even if you play devils advocate - references are not, for instance. Functions are not objects. – SergeyA Aug 15 '18 at 20:47
  • 3
    `template ` is the same a `template ` with the latter being less misleading/confusing IMHO. – NathanOliver Aug 15 '18 at 20:49
  • @RemyLebeau thx. But `typename` and `class` are interchangeable only in template declaration, aren't they? – Psyduck Aug 15 '18 at 20:53
  • 1
    Yes, only in templates, and only in that data-type context. You cannot write `typename MyClass { public: void DoFoo(); }`. For this, you have to use `class` or `struct`. – quetzalcoatl Aug 15 '18 at 20:54
  • Yet, I still cannot understand what you are trying to say by `what's the implementation of the data type in cpp`. – quetzalcoatl Aug 15 '18 at 20:56
  • @quetzalcoatl my understanding between `class` and `struct` is that `struct ` make all components public. Is there any other reasons why using `class` over `struct?` – Psyduck Aug 15 '18 at 20:57
  • @Noob. It's not revelant in this case. Since `class` in `template ` doesn't make `T` a class. – HolyBlackCat Aug 15 '18 at 21:05
  • Nope, struct and class are the same, the difference is only in 'make it public by default' vs 'make it private by default'. There's also a programming style that `struct` should be a POCO and for other thing `class` should be used, but that's just one of the styles not enforced/used by the language itself. And like HolyBlackCat said, it's not relevant in current context. You can use class/struct (but not typename) when defining a class, but you can use class/typename (but not struct) when defining a template. It's pretty confusing that the same words mean different things.. – quetzalcoatl Aug 15 '18 at 21:07
  • @quetzalcoatl I get it. And I edited my question, trying to make it clearer. – Psyduck Aug 15 '18 at 21:14

1 Answers1

4

inside <class T>, T should be a name of data type, and it's preceded by "class". Is this means the data types in CPP are also classes?

The answer is "no".

When defining a template, be it a class template or a function template, one can use typename as well as class. typename is the more accurate description but class is also supported, most likely for historical reasons.

Hence,

template <typename T> struct Foo {};

is the same as

template <class T> struct Foo {};

You can create objects by using any type as template parameter. It could be one of the fundamental types or one of the user defined types (aka classes/structs).

Give the above class template, one can use:

struct Bar {}; // User defined type

Foo<Bar> f1;   // Using user defined type to create the object f1
Foo<int> f2;   // Using a fundamental type to create the object f2
R Sahu
  • 204,454
  • 14
  • 159
  • 270
  • thanks. Just another question in the template declaration `template struct Foo {};`. The book I am reading says the complete template declaration would have a () before the template name, should it be `template struct Foo(params) {};`? Also, the `struct` here means the return type of the template would be a struct? – Psyduck Aug 15 '18 at 21:16
  • @Noob., that's the case for template specializations. See https://en.cppreference.com/w/cpp/language/template_specialization. The syntax that you have shown is not correct though. – R Sahu Aug 15 '18 at 21:18