Reputation: 3956
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));
}
Upvotes: 9
Views: 2310
Reputation: 5740
First of all, in case this isn't clear, the snippet as given by 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>
Now, the next two lines:
template <typename datadec, typename T>
inline auto ::datadec<Impl>::codec(const T& value)
constitute 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: 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: Without being provided more context by the OP, 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 reasonable explanation 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
.
Upvotes: 10