You can get the first element of the parameter package, for example this
template <typename... Elements>
struct type_list
{
};
template <typename TypeList>
struct type_list_first_element
{
};
template <typename FirstElement, typename... OtherElements>
struct type_list_first_element<type_list<FirstElement, OtherElements...>>
{
typedef FirstElement type;
};
int main()
{
typedef type_list<int, float, char> list;
typedef type_list_first_element<list>::type element;
return 0;
}
but it is impossible to pick up the last element, for example this
template <typename... Elements>
struct type_list
{
};
template <typename TypeList>
struct type_list_last_element
{
};
template <typename LastElement, typename... OtherElements>
struct type_list_last_element<type_list<OtherElements..., LastElement>>
{
typedef LastElement type;
};
int main()
{
typedef type_list<int, float, char> list;
typedef type_list_last_element<list>::type element;
return 0;
}
with gcc 4.7.1 complains:
error: 'type' in 'struct type_list_last_element <type_list <int, float, char →> does not name type
What sections of the standard describe this behavior?
It seems to me that template template packages are greedy in the sense that they consume all matching arguments, which in this case means that it OtherElementsconsumes all three arguments ( int, floatand char) and then there is LastElementnothing left for it, so the compilation failed. Am I correct in the assumption?
EDIT:
: , , , . , , - , , . .