Can Anybody explain what is basic syntax of given specialize template?
-
I want to know why after class name <R(Args...)> written ?
template <typename T> class Logger3; template <typename R, typename... Args> class Logger3<R(Args...)> { std::function<R(Args...)> func_; std::string name_;
-
Hi,
This means that this template class is specialized for a function that returns the R type and accepts Args... arguments. For example:
Logger3<int (const char *)>
For a more detailed explanation, see this Stack Overflow post.
-
Hi,
This means that this template class is specialized for a function that returns the R type and accepts Args... arguments. For example:
Logger3<int (const char *)>
For a more detailed explanation, see this Stack Overflow post.
@SGaist Thank you.
I need your help for further queries:
Can you provide me basic syntax for it because without basic syntax its difficult to digest because i am completely unaware theoretical concept. where someone say you need to pass justfunction return type (Arguments of function ) after class name
ex : template <typename R, typename... Args>
class Logger3 <R(Args...)>if i am not knowing basic syntax of it then how i can in future think of this type of logic?
-
In general you define a simple template class like this:
template<typename T> class A { };
This can then be used with
A<int> myA;
Sometimes you want to have a different implementation for a specific type which we call a template specialization:
template<> class A<int> { };
Just as with function overloading where the compiler picks the function with the best fit for the parameter types, the compiler will pick the most specific specialization.
Sometimes you don't want a specific type but some specific sort of type, e.g. a pointer:
template<typename T> class A<T*> { };
Now, if you write
A<int*> pInt;
orA<double*> pDouble;
it will match the specialization for pointers. In the same wayclass Logger3<R(Args...)>
will match any function as template argument. One of the "modern C++" features isArgs...
which means any number of types (0 or more). (The...
introduces a list of types.) So,R(Args...)
can matchvoid()
orvoid(int)
orint(double,int)
etc. Because we are just interested in the function signature this looks like a function declaration, but without any function name between the return type and the argument list. -