`decltype`给我一个对象的静态类型,还是它的运行时类型? [英] Does `decltype` give me an object's static type, or its runtime type?

查看:172
本文介绍了`decltype`给我一个对象的静态类型,还是它的运行时类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

decltype(e) 的定义如下:




  • 如果 e 未隐藏的 id-expression 或未加括号的类成员访问(5.2.5), decltype(e) code> e 。如果没有这样的实体,或者 e 命名一组重载的函数,程序是不成形的;

  • 如果 e 是xvalue, decltype(e) T&& c $ c>,其中 T e 的类型;

  • 否则,如果 e 是一个左值, decltype(e) T& ,其中 T e 的类型;

  • 否则 decltype(e) e 的类型。



decltype 说明符的操作数是未求值的操作数(第5条)。




第二,第三和第四种情况明确指的是表达式的类型,不包括任何多态性注意事项。



但是,我不完全确定实体是什么意思, case似乎是命名对象表达式 e

解决方案

对于实体类型是否意味着其运行时类型或其静态类型,



考虑:

  struct A {}; 
struct B:A {};

int main()
{
A * x = new B();
//什么是`decltype(* x)`?
}

使用 *

 <$ c> 

$ c> struct A {};
struct B:A {};

int main()
{
A& x = *(new B());
//什么是`decltype(x)`?
}

x 引用类型 A& ,并且其类型为

实体。



使用第一种情况的唯一方法是直接命名一个对象,我们不能以隐藏运行时类型的方式:

  struct A {}; 
struct B:A {void foo(){}};

int main()
{
A x = B(); //好吧,你现在切片了,innit?

decltype(x)y;
y.foo(); //错误:'struct A'没有名为'foo'的成员
}

strong>这就是为什么,根据这些答案,它总是静态类型


[C++11: 7.1.6.2/4]: The type denoted by decltype(e) is defined as follows:

  • if e is an unparenthesized id-expression or an unparenthesized class member access (5.2.5), decltype(e) is the type of the entity named by e. If there is no such entity, or if e names a set of overloaded functions, the program is ill-formed;
  • otherwise, if e is an xvalue, decltype(e) is T&&, where T is the type of e;
  • otherwise, if e is an lvalue, decltype(e) is T&, where T is the type of e;
  • otherwise, decltype(e) is the type of e.

The operand of the decltype specifier is an unevaluated operand (Clause 5).

The second, third and fourth cases clearly refer to the type of the expression, which would not include any polymorphism considerations.

However, and I'm not entirely sure what "entity" means here, the first case appears to be naming the object refered to by the expression e. It is ambiguous to me as to whether "the type of the entity" means its runtime type, or its static type.

解决方案

It's actually impossible to run into this problem, due to the restraints of that first case.

Consider:

struct A     {};
struct B : A {};

int main()
{
   A* x = new B();
   // What is `decltype(*x)`?
}

The use of * makes us fall through to the third case.

And for references?

struct A     {};
struct B : A {};

int main()
{
   A& x = *(new B());
   // What is `decltype(x)`?
}

x is a reference with type A&, and it is this "entity" whose type results.

The only way to use the first case is by directly naming an object, and we cannot do that in a way that hides a runtime type:

struct A     {};
struct B : A { void foo() {} };

int main()
{
   A x = B();     // well, you've sliced it now, innit?

   decltype(x) y;
   y.foo();       // error: ‘struct A’ has no member named ‘foo’
}

This is why, according to these answers, it is always the static type of an object that is used.

这篇关于`decltype`给我一个对象的静态类型,还是它的运行时类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆