Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/163.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 函数在不在命名空间中时工作,否则会中断_C++_Templates_G++ - Fatal编程技术网

C++ 函数在不在命名空间中时工作,否则会中断

C++ 函数在不在命名空间中时工作,否则会中断,c++,templates,g++,C++,Templates,G++,编译以下代码: #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::int

编译以下代码:

#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());
}
这似乎令人费解…名称空间不应该更改代码的功能?!?!?!?(或者应该如此吗?)

我正在使用std=c++14的
g++
。任何帮助都将不胜感激

为什么拥有
名称空间会改变什么


当Alf使用g++5.1.0编译此代码并删除out注释时,该编译器将报告第一个错误

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:11:5:错误:静态断言失败:索引超出范围 静态_断言(索引 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"); ^ foo.cpp:在“结构索引”的实例化中: foo.cpp:21:64:替换“使用IndexOf=typename索引的模板”时需要使用IndexOf=typename索引值[具有长-长无符号int i=i;第一个\u t=std::integral\u常量;其余\u t={std::integral\u常量,std::integral\u常量,std::integral\u常量,std::integral\u常量,std::integral\u常量}]' foo.cpp:52:38:从“main(int,char**)::[with auto:1=std::integral_constant]中必须输入” foo.cpp:33:15:从“void prototypeInd::templates::static_for(const Lambda&,std::integral_constant)[带长-长无符号int N=7ull;Lambda=main(int,char**):]递归需要” foo.cpp:33:15:void prototypeInd::templates::static_for(const Lambda&,std::integral_constant)[带长-长无符号int N=8ull;Lambda=main(int,char**):]中的必填项 foo.cpp:52:90:从这里开始需要 foo.cpp:11:5:错误:静态断言失败:索引超出范围 静态_断言(索引怎么了?您确定没有在该名称空间中隐藏另一个函数吗?请尝试交换名称空间中两个声明的顺序。@n.m.这很有效……(我可以问一下原因吗?@cheers-sandhth.-Alf当为
模板实例化一个
static\u时,对其内部的
调用
static\u是不合格的。针对在实例化点和模板声明点可见的
的所有static_声明进行解析。如果将其放在命名空间中,则第二个声明不可见(不起作用)。把它放在一个全局名称空间中,它是可见的(工作的)。@Cheersandhth.-Alf我错了,它在实例化时不可见,它有点复杂。编译器考虑在模板定义点可见的声明,以及来自与实例化上下文和定义上下文中的函数参数类型相关联的名称空间的声明。因此,在工作案例中,第二个声明是通过ADL查找找到的。现在clang和g++显然对ADL查找在这种情况下应该如何工作有不同的想法。。。 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"); ^