为什么这样写 g++会在 noexcept 那里报 expression cannot be used as a function 这样的错误?

2015-08-29 20:50:07 +08:00
 linux40
https://gist.github.com/anonymous/0cc5c77b5517826511b6
1330 次点击
所在节点    C
11 条回复
firemiles
2015-08-29 22:16:48 +08:00
expression 必须要能在编译时计算,函数在运行时计算,你在 expression 内嵌入了两个函数,所以这个 expression 也变成了 function ,而 noexcept 规定只能用于 expression 。
linux40
2015-08-29 23:09:06 +08:00
@firemiles 这个 noexcept 不会对表达式求值啊,为什么不会变成判断 T1 这个类型的接受一个 Other1 的构造函数抛不抛异常?
linux40
2015-08-29 23:11:02 +08:00
@firemiles 对于同类型可以,别的类型就不行了。。。
linux40
2015-08-29 23:15:58 +08:00
@firemiles 但是 operator=的话,其他类型可以。。。
bazingaterry
2015-08-30 00:21:47 +08:00
打开 gist 竟然提示

![]( )
firemiles
2015-08-30 09:20:10 +08:00
@linux40
template <typename T1, typename T2>
struct pair {
//...
template <typename Other1, typename Other2> constexpr
pair (Other1 &&o1, Other2 &&o2 ) noexcept (
noexcept (first (forward<Other1>(o1 ))) &&
noexcept (second (forward<Other2>(o2 )))
): first (forward<Other1>(o1 )), second (forward<Other2>(o2 )) {}
//...
};

forward<Ohter1>(o1 ) 应该不是一个 constexpr 吧,这样不是进行了一次函数调用吗
firemiles
2015-08-30 09:48:51 +08:00
@linux40 不好意思,测试了一下 noexcept 里调用函数确实可以,之前对 noexcept 理解有点问题。
firemiles
2015-08-30 09:57:19 +08:00
@linux40 我用 clang++可以编译通过,你换个编译器试试吧
linux40
2015-08-30 13:07:20 +08:00
@firemiles 是吗,那 g++就有我 3 、 4 楼说的情况,而 clang++就可以正常进行二楼所说的判断?
firemiles
2015-08-30 14:19:14 +08:00
@linux40

template <typename T1, typename T2>
struct pair {
//...
template <typename Other1, typename Other2> constexpr
pair (Other1 &&o1, Other2 &&o2 ) noexcept (
noexcept (T1 (std::forward<Other1>(o1 ))) &&
noexcept (T2 (std::forward<Other2>(o2 )))
): first (std::forward<Other1>(o1 )), second (std::forward<Other2>(o2 )) {}
//...
T1 first;
T2 second;
};
int main ()
{
pair<int, double> a (1, 1.2 );
}


我试着这么写可以通过编译,不知道满不满足你的要求,那两个 first 和 second 在 noexcept 里的我改成了 T1 和 T2 。
linux40
2015-08-30 19:17:01 +08:00
@firemiles 满足。。。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/216981

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX