为什么这段代码会输出 false?
A到B之间的“*this”的类型是A<int>,对吗?
//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;
}
输出:
$ g++ -std=c++11 this-type.cpp
$ ./a.out
false
A到B之间的“*this”的类型是A<int>,对吗?
this
是一个类型为A<int> *
的 prvalue 表达式。对this
进行间接引用会得到一个类型为A<int>
的 lvalue 表达式。表达式的类型并不总是与decltype
报告的相同。 - ecatmur