I do not understand the template defined below, can somebody help me decode it?
template <typename Impl>
template <typename datadec, typename T>
inline datadec<Impl>::codec(const T& value)
{
return codec<datadec>(Master::char_data(value), Master::size(value));
}
You may overload a function template either by a non-template function or by another function template. The function call f(1, 2) could match the argument types of both the template function and the non-template function.
Multiple parameters can be used in both class and function template. Template functions can also be overloaded. We can also use nontype arguments such as built-in or derived data types as template arguments.
Function templates are special functions that can operate with generic types. This allows us to create a function template whose functionality can be adapted to more than one type or class without repeating the entire code for each type. In C++ this can be achieved using template parameters.
Can there be more than one argument to templates? Yes, like normal parameters, we can pass more than one data type as arguments to templates.
First of all, in case this isn't clear, the snippet, as given in the OP, doesn't compile. In keeping with what I believe is the intention behind it, two minimal modifications are needed to make it compile:
template <typename Impl>
template <typename datadec, typename T>
inline auto ::datadec<Impl>::codec(const T& value)
{
codec<datadec>(Master::char_data(value), Master::size(value));
}
To answer the question, let's go over this line by line:
So, the datadec
class template takes the single template argument typename Impl
. Hence the first line:
template <typename Impl>
The next two lines:
template <typename datadec, typename T>
inline auto ::datadec<Impl>::codec(const T& value)
reveals itself to be a definition for a member function template codec()
of this class template datadec
(so it's a member function template of a class template). This function template itself takes two template arguments: typename datadec
and typename T
. Note that the first template argument here is of the same name as the class template itself -- datadec
.
Notice that in the OP there was a return value type missing from this function declaration.
Next we see what's inside the member function definition:
{
return codec<datadec>(Master::char_data(value), Master::size(value));
}
there's a call to a different codec()
, that is explicitly being used with a template argument that gets the datadec
template parameter passed from the outside and takes two non-template arguments: Master::char_data(value)
and Master::size(value)
.
Edit: In an attempt to shed light on the "dual role" of the datadec
name in this snippet, as it seems to raise some eyebrows that this argument, taken by the member function template, is of the same name as the class template itself (as noted above). Now, without being provided more context (in the form of code by the OP to accompany the snippet given), let's imagine, from a design perspective, that the class template datadec
represents some data decoder and that codec()
returns some codec related data. Then one possible example for these templated declarations to be as they are is that codec()
needs to know what type of data decoder it needs to use for its return value -- as in, you could specialize the two argument version of codec()
for different types of datadec
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With