Orient Orient - 17 days ago 6
C++ Question

Using sizeof template parameters pack in class template specialization

Check of correcteness during compilation of the following code (metafunction for selection of a type from a type list by right to left 1-based index) failed on GCC, whereas clang accepts this code:

#include <cstdlib>

template< std::size_t i, typename ...types >
struct at_index
{

};

template< typename first, typename ...rest >
struct at_index< (1 + sizeof...(rest)), first, rest... >
{
using type = first;
};

template< std::size_t i, typename first, typename ...rest >
struct at_index< i, first, rest... >
: at_index< i, rest... >
{

};

int main()
{
}


Which compiler is right?

GCC error message:

error: template argument '(1 + sizeof... (rest))' involves template parameter(s)
struct at_index< (1 + sizeof...(rest)), first, rest... >
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


I think the types from parameter pack in
sizeof...
operator should not be resolved into some parts of symbol names. Therefore no name mangling problems should be here.

Is this a clang extension to allow above code?

Answer

N4140 [temp.class.spec]/8.1:

A partially specialized non-type argument expression shall not involve a template parameter of the partial specialization except when the argument expression is a simple identifier.

This has since been relaxed by core issue 1315. Presumably GCC hasn't gotten around to implementing that yet.

Comments