The following code compiles:
#include "List.h"
//namespace prototypeInd{namespace templates{ //Uncomment for error
template <size_t N, typename Lambda>
inline void static_for(const Lambda& f,std::integral_constant<size_t,N>) {
static_for(f, std::integral_constant<size_t,N-1>());
f(std::integral_constant<size_t,N-1>());
}
template <typename Lambda>
inline void static_for(const Lambda& f, std::integral_constant<size_t,1>) {
f(std::integral_constant<size_t,0>());
}
//}} //Uncomment for error
//using namespace prototypeInd::templates; //Uncomment for error
template<size_t N>
using ic = std::integral_constant<size_t,N>;
typedef List<ic<0>,ic<1>,ic<2>,ic<3>,ic<4>,ic<5>,ic<6>,ic<7> > list;
int main(int argc,char** args){
static_for([](auto i){ std::cout << list::IndexOf<i>() << std::endl; },list::SizeOf());
}
But as soon as you uncomment the marked lines it gives a huge whopper of an error.
Here is List.h
:
template<class First, class... Rest>
struct SizeOf_ : std::integral_constant<size_t, SizeOf_<Rest...>() + 1>{};
template<class First>
struct SizeOf_<First> : std::integral_constant<size_t,1> { };
template<size_t index, class First, class... Rest>
struct Index_{
static_assert(index < SizeOf_<First,Rest...>(), "Index is outside of bounds");
typedef typename Index_<index - 1, Rest...>::value value;
};
template<class First, class... Rest>
struct Index_<0,First, Rest...>{
typedef First value;
};
template<class First_t, class... Rest_t>
struct List{
template<size_t i>
using IndexOf = typename Index_<i,First_t,Rest_t...>::value;
typedef SizeOf_<First_t, Rest_t...> SizeOf;
};
This seems inexplicable....A namespace should not change the function of the code?!?!?!? (or should it?).
I am using g++
with std=c++14....Any help is appreciated
Why does having a namespace
change anything?
When Alf compiles this code with g++ 5.1.0 and removes the out-commenting, that compiler reports as first error that
foo.cpp:11:5: error: static assertion failed: Index is outside of bounds static_assert(index < SizeOf_<First,Rest...>(), "Index is outside of bounds");
The complete diagnostics avalanche for that first error:
foo.cpp: In instantiation of 'struct Index_<18446744073709551615ull, std::integral_constant<long long unsigned int, 0ull>, std::integral_constant<long long unsigned int, 1ull>, std::integral_constant<long long unsigned int, 2ull>, std::integral_constant<long long unsigned int, 3ull>, std::integral_constant<long long unsigned int, 4ull>, std::integral_constant<long long unsigned int, 5ull>, std::integral_constant<long long unsigned int, 6ull>, std::integral_constant<long long unsigned int, 7ull> >': foo.cpp:21:64: required by substitution of 'template<class First_t, class ... Rest_t> template<long long unsigned int i> using IndexOf = typename Index_<i, First_t, Rest_t ...>::value [with long long unsigned int i = i; First_t = std::integral_constant<long long unsigned int, 0ull>; Rest_t = {std::integral_constant<long long unsigned int, 1ull>, std::integral_constant<long long unsigned int, 2ull>, std::integral_constant<long long unsigned int, 3ull>, std::integral_constant<long long unsigned int, 4ull>, std::integral_constant<long long unsigned int, 5ull>, std::integral_constant<long long unsigned int, 6ull>, std::integral_constant<long long unsigned int, 7ull>}]' foo.cpp:52:38: required from 'main(int, char**):: [with auto:1 = std::integral_constant<long long unsigned int, 18446744073709551615ull>]' foo.cpp:33:15: recursively required from 'void prototypeInd::templates::static_for(const Lambda&, std::integral_constant<long long unsigned int, N>) [with long long unsigned int N = 7ull; Lambda = main(int, char**)::]' foo.cpp:33:15: required from 'void prototypeInd::templates::static_for(const Lambda&, std::integral_constant<long long unsigned int, N>) [with long long unsigned int N = 8ull; Lambda = main(int, char**)::]' foo.cpp:52:90: required from here foo.cpp:11:5: error: static assertion failed: Index is outside of bounds static_assert(index < SizeOf_<First,Rest...>(), "Index is outside of bounds"); ^