Why this code produces a false output?
//this-type.cpp
#include <iostream>
#include <type_traits>
using namespace std;
template<typename testype>
class A
{
public:
A()
{
cout << boolalpha;
cout << is_same<decltype(*this), A<int>>::value << endl;
}
};
class B : public A<int>
{
};
int main()
{
B b;
}
Output:
$ g++ -std=c++11 this-type.cpp
$ ./a.out
false
The type of "*this" inside A through B is A< int >, isn't it?
Are you sure
decltype(*this)
is A ? You should investigate on that with an uglycout
debug line.Try:
and maybe
remove_cv
in some other contexts (if you don't care aboutconst
/volatile
) like this:*this
is an lvalue of typeA
, sodecltype(*this)
will give the reference typeA &
. Recall thatdecltype
on an lvalue gives the reference type:Output: