鉴于类型的定义A:
struct A { int i; };
Run Code Online (Sandbox Code Playgroud)
根据规范[expr.ref](我使用的是n4618):
(如果
E2是非引用,)...如果E1是左值,那么E1.E2是左值; 否则E1.E2是xvalue ...
显然A{}.i是xvalue; 还给出了[dcl.type.simple]:
(for
decltype(e),) - ...如果e是未加密码的 id-expression或未加括号的类成员访问... - 否则,如果e是xvalue,则decltype(e)是T &&,其中T是类型e
因此,decltype( ( A{}.i ) )将产生int &&.
但是我尝试了GCC5.1和Clang3.9,它们产生了int,而vs2015u3产生了int &&.哪个是对的?
表达式x->y需要x是指向完整类类型的指针,或者何时x是类的实例,需要operator->()定义x.但是当后者是这种情况时,为什么不能使用转换函数(x即将对象转换为指针)?例如:
struct A
{
int mi;
operator A*() { return this; }
};
int main()
{
A a;
a[1]; // ok: equivalent to *(a.operator A*() + 1);
a->mi; // ERROR
}
Run Code Online (Sandbox Code Playgroud)
这会给出一条错误消息:
error: base operand of '->' has non-pointer type 'A'
但问题是,为什么不使用它a.operator A*(),就像它一样a[1]?
C++不允许定义函数类型数组的基本原理(技术或概念)是什么?例如
using fn_t = int(int);
fn_t ary[2]; // ERROR (ref: ISO/IEC-14882 [dcl.array])
Run Code Online (Sandbox Code Playgroud)