The following code was compiled with VC++ Nov 2012 CTP. But the compiler gave a warning.
I just wonder whether this is a bug of VC++ Nov 2012 CTP.
struct A
{
int n;
A(int n)
: n(n)
{}
int Get() const
{
return n;
}
int Get()
{
//
// If using "static_cast<const A&>(*this).Get();" instead, then OK.
//
return static_cast<const decltype(*this)&>(*this).Get(); // Warning!
}
};
int main()
{
A a(8);
//
// warning C4717: 'A::Get' : recursive on all control paths,
// function will cause runtime stack overflow
//
a.Get();
}
decltype
applied to an expression that's not an id-expression gives you a reference, sodecltype(*this)
is alreadyA&
, and you can't make thatconst
again. If you really wanted to usedecltype
, you could do something like this:Or even this:
Of course it's much simpler to just say
static_cast<A const &>(*this)
.