関連する質問:template-function-is-same-in-template-classes
ポインタのタイプ「this」(gcc 4.7.2、c ++ 11)に少し戸惑っています。原則として、たとえばタイプCの非constオブジェクトのポインタ「this」のタイプは「C * const」であるため、「*this」のタイプは「C」です。しかし、「is_same」クラスの動作は私を混乱させました。
テスト:
// this-type.cpp
#include <iostream>
#include <type_traits>
using namespace std;
class C
{
public:
void test()
{
cout << boolalpha;
cout << "'this' const?" << " "
<< is_const<decltype(this)>::value << endl;
cout << "'*this' const?" << " "
<< is_const<decltype(*this)>::value << endl;
cout << "'*this' and 'C' the same?" << " "
<< is_same<decltype(*this), C>::value << endl;
cout << "'this' and 'C*' the same?" << " "
<< is_same<decltype(this), C*>::value << endl;
}
};
int main()
{
C c;
c.test();
}
出力:
$ g++ --version | grep g++
g++ (Ubuntu/Linaro 4.7.2-2ubuntu1) 4.7.2
$ g++ -std=c++11 this-type.cpp
$ ./a.out
'this' const? false
'*this' const? false
'*this' and 'C' the same? false
'this' and 'C*' the same? true
ただし、期待される出力は次のとおりです。
$./a.out
'this' const? true // C* const
'*this' const? false // C
'*this' and 'C' the same? true
'this' and 'C*' the same? false // C* const vs. C*
ここで何が起こったのですか?