[问题] overload 加上 namespace 之后会 fail

楼主: johnjohnlin (嗯?)   2014-07-31 15:23:09
呃我又来问问题了......
开发平台(Platform): (Ex: VC++, GCC, Linux, ...)
gcc version 4.8.2 (Ubuntu 4.8.2-19ubuntu1)
有用到 C++11,不过应该跟 C++11 没关
问题(Question):
加上 namespace 之后 compiler 会到达编译 template 深度上限
不过我想问的是为什么不加的时候不会(见下面程式码)
我有上网找过资料了,有一些类似的情形
可是好像又不太一样
喂入的资料(Input):
N/A
预期的正确结果(Expected Output):
加不加 namespace 都一样(见下面程式码)
错误结果(Wrong Output):
如问题描述
使用 g++ -ftemplate-depth=15 -std=c++11 编译可通过
加上 -DMAKE_FAIL (放入 namespace 的使用)之后就会炸掉
程式码(Code):(请善用置底文网页, 记得排版)
http://pastebin.com/nAwHMeCr
黄色是加上 namespace 的时候用
1 #include <type_traits>
2 #include <iostream>
3
如果用 namespace 包起来
就不能编译
4 #ifdef MAKE_FAIL
5 namespace detail {
6 #endif
7
这个 template 编译期递回呼叫自己
在没有 namespace 的时候可以动
不过我觉得不加 namespace 的时候能编译才是诡异的状况
因为他可以用 template 自己本身实现下一层递回
所以进编译期的无穷循环才是合理
8 template<
9 int CUR_IDX,
10 class PtrType,
11 class Func,
12 class LeftDistance
13 >
14 void _set_nth_element(
15 PtrType p,
16 Func f,
17 LeftDistance
18 )
19 {
20 _set_nth_element<CUR_IDX+1>(
p, f 直接传下去
21 p, f,
把剩多远传下去,如果剩下 0 会 match 下面那个 template
22 std::integral_constant<int, LeftDistance::value-1>()
23 );
24 }
25
用 overload 宣告上面那个函数递回尾端的 case
integral_constant 0 的 时候就会 match
26 template<
27 int CUR_IDX,
28 class PtrType,
29 class Func
30 >
31 void _set_nth_element(
32 PtrType p,
33 Func f,
34 std::integral_constant<int, 0>
35 )
36 {
37 f(p+CUR_IDX);
38 }
39
40 #ifdef MAKE_FAIL
41 }
42 #endif
43
理论上等价于 f(p+N),N 为编译期常数
透过呼叫 _set_element_nth 来达成
44 template<
45 int N,
46 class PtrType,
47 class Func
48 >
49 void set_nth_element(
50 PtrType p,
51 Func f
52 )
53 {
54 #ifdef MAKE_FAIL
55 using namespace detail;
56 #endif
57 _set_nth_element<0>(p, f, std::integral_constant<int, N>());
58 }
59
60 int main(int argc, char const* argv[])
61 {
62 int p[10] = {};
63
64 std::cout << p[7] << std::endl;
65 set_nth_element<7>(p, [] (int *p) -> void {
66 *p = 100;
67 });
68 std::cout << p[7] << std::endl;
69 return 0;
70 }
作者: carylorrk (carylorrk)   2014-07-31 15:31:00
看得痛苦 程式可以分开来贴在 ideone 吗...未看先猜会不会加了 namespace 找不到特化的 baseline?
楼主: johnjohnlin (嗯?)   2014-07-31 15:37:00
我有贴程式码连结在上面为什么会找不到特化,不是在同一个 namespace 下面?应该是问为什么加了 namespace 之后能找到的就不一样
作者: jackace (inevitable......)   2014-07-31 15:56:00
不加namespace: instantialize的时候在global中展开, 两个

Links booklink

Contact Us: admin [ a t ] ucptt.com