テンプレート・メタ・プログラミング中に良くハマるので、時々typeid().name()を使って型を可視化してデバッグしています。(gccの時は、デマングルしてます。)
その際に、気がついた(ハマった)のですが、typeid()は型にconstや参照がついていても、それを外してしまいます。
例えば、下記のようになります。
C++
1#include <typeinfo> 2#include <iostream> 3 4template<typename tType> 5struct Helper { }; 6 7int main() 8{ 9 std::cout << std::boolalpha; 10 11 std::cout << "int : int const = " << (typeid(int) == typeid(int const)) 12 << " (" << (typeid(Helper<int>) == typeid(Helper<int const>)) << ")\n"; 13 14 std::cout << "int : int& = " << (typeid(int) == typeid(int&)) 15 << " (" << (typeid(Helper<int>) == typeid(Helper<int&>)) << ")\n"; 16 17 std::cout << "int : int const& = " << (typeid(int) == typeid(int const&)) 18 << " (" << (typeid(Helper<int>) == typeid(Helper<int const&>)) << ")\n"; 19 20 std::cout << "int* : int const* = " << (typeid(int*) == typeid(int const*)) 21 << " (" << (typeid(Helper<int*>) == typeid(Helper<int const*>)) << ")\n"; 22 return 0; 23} 24 25実行結果: 26int : int const = true (false) 27int : int& = true (false) 28int : int const& = true (false) 29int* : int const* = false (false)
typeid()
はint
とint const
, int&
, int const&
を全て同じ「型」として認識しています。
しかし、テンプレート・パラメータとして指定した場合は異なりますので、C++コンパイラもこれらは異なる型として認識している筈です。
MSVC 2015とMinGW 4.9.2で同じ結果になりましたので、恐らくC++の仕様だろうと予想しています。
なぜ、このようにある意味「変な」仕様なのか、ご存知の方いらっしゃいませんか?
適切な目的、もしくは、歴史的経緯があるのだろうとは思うのですが。

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/12/05 06:29 編集