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 1 year ago.
Improve this question
What does this syntax mean?
ForExample<Something>();
Can someone explain with a few examples how and what for it can be used?
This code could be a call of the template function or instantiation of temporary class object.
For example, function is defined as:
template <typename T>
void ForExample(){
// Do something
}
This function can be called as:
ForExample<int>();
In your case type Something can be any type (int, double, string, float ...)
Or we can define template class:
template <typename T>
class ForExample {
// something
};
Temporary object can be created as:
ForExample<int>();
Related
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 2 years ago.
Improve this question
Consider the following function:
void f(...);
And the following structure:
template <class T>
struct s;
template <class R, class...A>
struct s<R(A...)>
{
//...
};
How exactly does s<decltype(f)> get treated, considering its variadic argument type?
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 5 years ago.
Improve this question
I find this question tricky. What do u ppl think?
template <typename T> class myTemplate
{
public:
T val;
...
};
void myFunction()
{
MyTemplate<int> a;
MyTemplate<double> b;
}
You are instantiating the template twice with two different template parameters, so it'll create two class instances.
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
I'm creating class that has a templated object (Item<T>) as a member, basically like this:
class myClass
{
int other_int;
public:
int member_function();
vector<Item<T>> vec;
};
Currently, I have Item<string>, but I need to be able to use it with non string objects. Is there a way to do this without templating myClass (which would obviously be a lot of work for a complicated class)?
If your class will only use Item< string>, you may try:
class myClass
{
int other_int;
public:
int member_function();
vector<Item<string>> vec;
};
But if you want any other type of Item in the vector, the answer is No, there is no magic solutions.
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.
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;