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.
Related
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>();
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.
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
I often write code where I have the following scenario.
I have a (pure) function f and another function g which is templated so that it takes a callable object, say with similar type as f, as an argument.
Now often f is not super trivial but a couple of lines long and since it
is it is stateless I define f as a normal function. But then when passing f I have to write &f and I really dislike the syntax.
Another way would be to write a global lambda or a functor, that would fix my "& issue". But syntactically I prefer normal functions...
So is there a way to define g such that I can pass f as a normal function but avoid the &?
You do not have to explicitly take the address of a function when passing it. As an example, the following code compiles and works:
template <typename F>
void foo(F f) { f(); }
void bar() { }
int main()
{
foo(bar);
}
live example on wandbox.org
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
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 7 years ago.
Improve this question
Getting ready for an exam and passed upon this question that I cannot confidently answer.
Say we need to implement a Stack using STL deque and an Adapter design, which of the following is the right approach: (There can be only one answer)
1. template <typename T> class stack: public deque<T> {};
2. template <typename T> class stack {private: deque<T>* dptr; };
3. template <typename T> class stack {protected: deque<T>* dptr; };
4. template <typename T> class stack {private: deque<T> {};}
5. template <typename T> class stack: private deque<T> {};
6. template <typename T> class stack {protected: deque<T> d; };
Any tips for the solution would be appreciated. Thanks.
You never want to publicly inherit from an STL container. They're simply not designed for it because for "pay as you go" reasons they have no virtual destructor. Other forms of inheritance are simply creating unnamed members which won't serve you any purpose in this case (they can be useful for using C APIs etc). So that rules out 1 and 5. So now you can have either a private or protected member. Choose protected because that leave options open for other classes inheriting from you. So that leaves 3 and 6. Now do you want by value or a pointer. This is an STL container that can happily manage its own resources when destructed, unlike a raw pointer. So 6 is the clear winner.