【发布时间】:2019-03-02 09:10:15
【问题描述】:
我不确定是否完全理解[dcl.type]/4.3:
对于表达式
e,decltype(e)表示的类型定义如下:
- [...]
- (4.3) 否则,如果
e是未加括号的id 表达式或未加括号的类成员访问,则decltype(e)是e命名的实体的类型。如果不存在这样的实体,或者如果e命名了一组重载函数,则程序格式错误;- [...]
对我来说,强调的部分都适用于id-expression和类成员访问,对吧?
使用我最喜欢的编译器,我得到以下结果。
✓ 被编译器接受
namespace N { void f() {} }
using type = decltype(N::f);
type* pf = N::f;
好吧,我猜; N::f 是一个不带括号的 id 表达式,没有命名一组重载函数。
✗ 被编译器拒绝
namespace N { void f() {} void f(int) {} }
using type = decltype(N::f); // error: decltype cannot resolve address of overloaded function
type* pf = N::f;
好的; N::f 确实命名了一组重载函数。
✗ 被编译器拒绝
struct S { void f(){} };
using type = decltype(S::f); // error: invalid use of non-static member function 'void S::f()'
type* pf = &S::f;
嗯? S::f 会命名一组一个重载函数吗?
总而言之,我对[dcl.type]/4.3的理解是不是很差? gcc主干错了吗?两个都?没有?卡莫乐?
【问题讨论】:
标签: c++ c++14 language-lawyer c++17 decltype