以下代码编译:
#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());
}
但是一旦你取消注释标记的线条,它就会给出一个巨大的误差。
以下是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;
};
这似乎莫名其妙......命名空间不应该改变代码的功能?!?!?!? (或应该?)。
我正在使用g++
和std = c ++ 14 ....感谢任何帮助
为什么namespace
改变了什么?
当Alf使用g ++ 5.1.0编译此代码并删除out-commenting时,该编译器报告为第一个错误
foo.cpp:11:5: error: static assertion failed: Index is outside of bounds static_assert(index < SizeOf_<First,Rest...>(), "Index is outside of bounds");
针对第一个错误的完整诊断雪崩:
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"); ^