Regarding Templates in c++ [closed] - c++

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.

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

What is `using`, and what is the colon after the constructor in C++ [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Could you help me to understand the code below? I have an interface and need to write a code. But it first time when I work with OOP. I would be grateful if you answer some questions about this code.
template <class T, class Compare = std::less<T>>
class List_of_objects {
public:
using IndexChange =
std::function<void(const T& element, size_t new_element_index)>;
explicit Heap(
Compare compare = Compare(),
IndexChange index_change = IndexChange());
// Other methods not important in my question
private:
IndexChange index_change_;
Compare compare_;
}
// Realization of methods
template<class T, class Compare>
List_of_objects<T, Compare>::List_of_objects(Compare compare, IndexChange index_change)
: compare_(compare), index_change_(index_change) {}
I have learned what std::function<> do from this site but I don't understand why we are writing using before 'IndexChange'
Also I have no idea what does compare_(compare) means, and what mean single colon in realization of List_of_objects()
I think that it is not difficult questions but I meet a deadline, I can't find answers in Stroustrup and in i-net:(
Thank you for any help!
using IndexChange = std::function<void(const T& element, size_t new_element_index)>;
permits you not to write the definition another time. This is a shortcut.
Then
IndexChange index_change_;
is the same as defining
std::function<void(const T& element, size_t new_element_index)> index_change_;
See : What is the logic behind the "using" keyword in C++?
Concerning compare_, it is the attribute where its type is defined by the template argument Compare in your definition, and by default it is std::less.
So calling compare_() will call an instance of std::less or another class if you instantiate List_of_objects with another template argument. As for example : List_of_objects<int, MyComparator> then the class MyComparator will be used instead of std::less.
See : Default template arguments for function templates
The part with the semicolons will initialize the members of your class.
See : In this specific case, is there a difference between using a member initializer list and assigning values in a constructor?
using IndexChange =
std::function<void(const T& element, size_t new_element_index)>;
You can understand this as an abbreviation for a type. Instead of having to write std::function<..... you can simply use IndexChange, as it is done here:
explicit Heap(
Compare compare = Compare(),
IndexChange index_change = IndexChange());
This is a constructor for List_of_objects:
List_of_objects<T, Compare>::List_of_objects(Compare compare, IndexChange index_change)
: compare_(compare), index_change_(index_change) {}
the part after the single colon initializes members with the parameters passed to the constructor.

c++ multiple function on 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 8 years ago.
Improve this question
New to c++ - kind of been cramming the last few days. Things are going pretty well! I do have one question though.
If I make a template:
template <class T>
T testFunc(T t1, T t2)
{
// code code code code
}
My question, is the template <> line specific to that ONE function underneath? I couldn't continue to use the T placeholder in further functions could I?
like:
template <class T>
T testFunc(T t1, T t2)
{
// code code code code
}
T testFunc2(T t1, T t2)
{
// This one does other things....;
}
Well when you declare
template <class T>
T testFunc(T t1, T t2)
This means that you are declaring a generic function "testFunc" which takes any class and works with it. To be honest, I don't really know how it can be useful. If you are defining your function in the global namespace then you can use as many template 's above your functions as you like. They are in now way connected.
You can make a generic class that takes another class as a template. That way you don't have to write "template " above every single method in your class. But your methods CAN take the type T as an argument.

Template class name with <> mark [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<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;
.....
}