as an helper for an array class I am trying to count the depth of nested initializer_lists. This works most of the time but there is an exceptional case that is fundamental for the correct behaviour.
The isolated code looks like this:
#include <iostream>
using std::size_t;
using std::initializer_list;
template<typename T, size_t D>
struct TNestedInitializerList
{
using nested_list = initializer_list<typename TNestedInitializerList<T, D - 1>::nested_list>;
};
template<typename T>
struct TNestedInitializerList<T, 1>
{
using nested_list = initializer_list<int>;
};
template<typename T, size_t D>
using NestedInitializerList = typename TNestedInitializerList<T, D>::nested_list;
class NestedCounter
{
public:
NestedCounter()
{
std::cout << "depth = 0" << std::endl;
}
NestedCounter( NestedInitializerList<int, 1> )
{
std::cout << "depth = 1" << std::endl;
}
NestedCounter( NestedInitializerList<int, 2> )
{
std::cout << "depth = 2" << std::endl;
}
NestedCounter( NestedInitializerList<int, 3> )
{
std::cout << "depth = 3" << std::endl;
}
NestedCounter( NestedInitializerList<int, 4> )
{
std::cout << "depth = 4" << std::endl;
}
};
int main( int argc, char** argv )
{
NestedCounter nc0;
NestedCounter nc1( {{{{2,3}}}} );
NestedCounter nc2( {{1, 2},{3, 4}} );
// NestedCounter nc3( {{1},{2}} ); // fails because of ambigouity. Maybe it's confused with 'int i{3};' constructors?
NestedCounter nc4( {2,3,4,5,6,7} );
return 0;
}
I commented the line that fails (variable nc3). The only difference to the declaration of nc2 is that there is no seperating comma between the values. I guess there is some syntactic confusion between the initializer_list and the constructor of an int. Maybe someone ran into a similiar case and knows a way to fix it?
Thanks in advance!
Aucun commentaire:
Enregistrer un commentaire