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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…