I'm attempting to solve a problem in which decltype
will greatly simplify things, but I'm running into an issue using decltype
on *this
and adding a const
qualifier. The sample code below demonstrates the problem.
#include <iostream>
struct Foo
{
void bar()
{
static_cast<const decltype(*this)&>(*this).bar();
}
void bar() const
{
std::cout << "bar" << std::endl;
}
};
int main(int argc, char* argv[])
{
Foo f;
f.bar(); // calls non-const method
return 0;
}
The code compiles in MSVC2010, but execution recurses until a stack overflow occurs.
Ideone reports compiler error
prog.cpp: In member function 'void Foo::bar()':
prog.cpp:7:38: error: 'const' qualifiers cannot be applied to 'Foo&'
If I change the line
static_cast<const decltype(*this)&>(*this).bar();
to
static_cast<const Foo&>(*this).bar();
it works as expected.
Am I misusing or misunderstanding decltype?