Why is the definition of variation of a variational pattern not compiled?

Using gcc 4.7.3, I get the following error

prog.cpp: In the function 'int main (): prog.cpp: 27: 63: error: "Erase" :: The result was not declared

with this code :

template <typename... List> struct TypeList { enum { Length = sizeof...(List) }; }; template <typename ToErase, typename... List> struct Erase; template <typename ToErase> struct Erase<ToErase, TypeList<>> { typedef TypeList<> Result; }; template <typename ToErase, typename... Head, typename... Tail> struct Erase<ToErase, TypeList<Head..., ToErase, Tail...>> { typedef TypeList<Head..., Tail...> Result; }; int main() { static_assert(Erase<double, TypeList<int, double, char>>::Result::Length == 2, "Did not erase double from TypeList<int, double, char>"); return 0; } 

I do not understand why the code does not compile if the received error message is received, given that a similar case compiles:

 template <typename ToAppend, typename... List> struct Append; template <typename ToAppend, typename... List> struct Append<ToAppend, TypeList<List...>> { typedef TypeList<List..., ToAppend> Result; } template <typename... ToAppend, typename... List> struct Append<TypeList<ToAppend...>, TypeList<List...>> { typedef TypeList<List..., ToAppend...> Result; } 

Is there a quote from the standard about the impossibility of outputting elements in the middle of two parameter packages, as I try to do with the first block of code?

+2
c ++ c ++ 11 variadic-templates
source share
1 answer

& section; 14.8.2.5 (Deduction of template arguments from type), clause 5 lists the contexts in which template arguments cannot be inferred. The corresponding number is the last in the list:

- a package of function parameters that does not occur at the end of a parameter-declaration-parameter.

So in:

 struct Erase<ToErase, TypeList<Head..., ToErase, Tail...>> 

Head cannot be inferred; this does not occur at the end of the parameter list.

Unlike:

 struct Append<TypeList<ToAppend...>, TypeList<List...>> 

Both ToAppend and List appear at the end of their respective parameter lists and therefore can be displayed.

+3
source share

All Articles