Template class name with <> mark [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
template<int32_t ID= 0, class ID_TYPE = int32_t>
class Event {
public:
typedef ID_TYPE type;
typedef Event<ID, ID_TYPE> event_obj_type;
.....
}
About this code, what does "Event<ID, ID_TYPE>" mean?
class name with <>, i never seen this before.
Could anybody enlighten me?
thanks

You are probably familiar with function templates. This:
Event<ID, ID_TYPE>
is a class template. Unlike function template instantiation, where arguments of function itself helps the compiler to deduce template type arguments, with class templates you must explicitly pass template type (in angle brackets).
An Idiot's Guide to C++ Templates - Part 1

Event is a template : a pattern for creating similar classes at compile time.
If at some places of your code you make :
Event<0, int32_t> A;
Event<3, char> A;
Two classes will be defined from this template. One with ID = 0, ID_TYPE = int32_t and the other with ID = 3, ID_TYPE = char.
[EDIT]
If you decompose the first one, your class definition will be something like :
class Event
{
public:
typedef int32_t type;
typedef Event event_obj_type;
.....
}

Related

Is there good practices for where to declare using in C++? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
Is there good pratices to declare a using (for typedef like usage) ?
For example if I have a class with a std::array<std::array<T, W>, H> member and I want to ease both reading and writing with a using like this
template<typename T, uint32_t H, uint32_t W>
using matrix = std::array<std::array<T, W>, H>;
Where should I put this ? Inside the class declaration or outside or even in a separate header file ?
Thanks
Your type alias template has exactly the same purpose than a template class, i.e. define a type:
template<typename T, uint32_t H, uint32_t W>
using matrix = std::array<std::array<T, W>, H>;
int main() {
matrix<double, 10,3> m;
return 0;
}
So the good practice would be to handle it exactly as you would do with other type definitions:
Put it in a header if you intend to reuse this definition in many places (and it seems so for your example, since a matrix is something rather general);
Embed it in a class (probably in a header) if it's an implementation detail that has not a general purpose;
Put it in the compilation unit where you use it, if you use your matrix in a single source file.
For non-template type alias, it's the same principle as with the old typedef, so exactly the same as above, and in addition,
Put it in a function body, if it has the sole purpose of serving as a very local shortcut for a very long type name.

CPP:Is generic data type T a class when defining template [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
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?
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

Deciphering C++ code: function in struct with same name as the struct [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 8 years ago.
Improve this question
I am trying to pick up a programming. I got this code from ai-junkie.com. I don't understand the last lines of each struct
SNeuron(int NumInputs),
SNeuronLayer(int NumNeurons, int NumInputsPerNeuron).
What is the purpose of this? Does anyone care to teach basic c++?
struct SNeuron {
//the number of inputs into the neuron
int m_NumInputs;
//the weights for each input
vector<double> m_vecWeight;
//ctor
SNeuron(int NumInputs);
};
struct SNeuronLayer {
//the number of neurons in this layer
int m_NumNeurons;
//the layer of neurons
vector<SNeuron> m_vecNeurons;
SNeuronLayer(int NumNeurons, int NumInputsPerNeuron);
};
It's a constructor. And someone has applied a pointless comment ctor to it. It allows you to instantiate an instance of SNeuron using code like
SNeuron sn(5);
This helps program stability. In C, you'd have to populate the structure fields yourself having instantiated an instance of the structure. That can leave a structure in an ill-defined state. In C++, the instance can be fully created in one step.
Remember that in C++, a struct is exactly the same as a class: with the exception that in a struct all member functions and member data are public by default (whereas in a class they are private by default).
This is a C-tor for the struct SNeuron that accepts an int.
in C++, a struct is simply a class with all members public by definition. And like any class it can have a c-tor, d-ctor and other C++ elements.

Regarding Templates in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
this might be a silly question but it's very important to me , tomorrow is my final exam in Object-Oriented C++ , anyways my teacher said something very important (That I can't remember) about when I am supposed to specify variables or data types when I am making an object, it's something like < int , int , int , double , int > .. everything on the left side must be the same type or the opposite I can't remember it, please if anyone got the idea about what I am talking about PLEASE help me <3 am desperate
Editing:
ugh I'm very sorry but I am really scared from this exam and this thing is important :( anyways when I am making an object from a class that has templates it goes like this Class <.int, int, double.> obj1 , right ? these data type in the middle, there's a rule about them that says if the first type is integer then everything on it's right or left must be the same type, or something like that, that's what I can't remember
A class template declaration consists of a name and the template parameters. The template parameters come first, and are written within angle brackets, like this:
template <typename Foo, typename Bar>
class Gizmo
{
};
At this point, Foo and Bar aren't real types -- they are placeholders for real types you will provide when you instantiate the Gizmo.
Instantiating a class template starts with the class name, followed by <, and then a comma-seperated list of template parameters, followed finally by a > and your variable name:
Gizmo<int, double> myGizmo;
these data type in the middle, there's a rule about them that says if
the first type is integer then everything on it's right or left must
be the same type, or something like that, that's what I can't remember
No, there is no such rule. Any of the template parameters above could be any type. The following are all legitimate:
Gizmo<int, double> g1;
Gizmo<int, std::string> g2;
Gizmo<std::string, int> g3;
Gizmo<double, char> g4;
What you might be thinking of is default parameters. With class templates you can specify some defaults for the template parameters:
template <typename Foo=int, typename Bar=double>
class Gizmo
{
};
However, if you do specify a default template parameter, then all subsequent template parameters must also have defaults. So this is OK:
template <typename Foo, typename Bar=int>
class Gizmo
{
};
...but this is not:
template <typename Foo=int, typename Bar>
class Gizmo
{
};
because there is a default for Foo, but not for Bar.

is template <typename T=char> correct? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
template <typename T=char>
I saw this code in some porjects.
Why does he/she put "=char" their?
Is this legal, How does it mean?
thank you
It's a default value for the template parameter, so it can be instantiated without having to specify that type.
template <typename T=char>
class Foo { ... };
Foo<> x; // a Foo<char>
Foo<char> y; // same as above
Foo<int> z; // now a Foo<int>
It is used to set default values or types for class template parameters.
template <class T=char, int N=10> class mysequence {..};
We could create objects using the default template parameters by declaring:
mysequence<> myseq;
Which would be equivalent to:
mysequence<char,10> myseq;