※ ChatGPTを利用し、要約された質問です(原文:c++11での文字列リテラルの特殊化について)
このQ&Aのポイント
c++11言語でのテンプレート部分特殊化についての質問です。
型名を直接記述したD,G、文字列リテラルを記述したE,H。コンパイラ毎の差はあれど、GとHの型名は同じものが表示されます。
[D:3] [E:1]と値は違い、別の特殊化テンプレートが使われています。この部分が分かりません。また、配列リテラル、文字列リテラルに対し部分特殊化テンプレートを宣言する方法などありましたら、ご教示お願いします。
c++11言語でのテンプレート部分特殊化についての質問です。
コメントアウト部分は出力結果です
template<class T> struct VT { static const int type = 1;};
template<class T,int N> struct VT< T[N] > { static const int type = 2;};
template<class T,int N> struct VT< const T[N] > { static const int type = 3;};
template<class T> struct VT< T* > { static const int type = 4;};
template<class T> struct VT< const T*const > { static const int type = 5;};
#include<iostream>
#include<typeinfo>
int main(){
std::cout<<"A:"<< VT< char >::type << std::endl; // A:1
std::cout<<"B:"<< VT< char[10] >::type << std::endl; // B:2
std::cout<<"C:"<< VT< char* >::type << std::endl; // C:4
std::cout<<"D:"<< VT< char const [1] >::type << std::endl; // D:3
std::cout<<"E:"<< VT< decltype("") >::type << std::endl; // E:1
std::cout<<"G:"<< typeid( char const [1] ).name() << std::endl;// G:char const [1]
std::cout<<"H:"<< typeid( "" ).name() << std::endl;// H:char const [1]
}
型名を直接記述したD,G、文字列リテラルを記述したE,H。
コンパイラ毎の差はあれど、GとHの型名は同じものが表示されます。
ですが、[D:3] [E:1]と値は違い、別の特殊化テンプレートが使われています。
この部分が分かりません。
また、配列リテラル、文字列リテラルに対し部分特殊化テンプレートを宣言する方法などありましたら、ご教示お願いします。
お礼
返答ありがとうございます VT<T &> ただの参照型、失念していました。 VT<T (&)[N]> 試行錯誤の時、T[N]& などと記述しエラーが出ていたので不可能だと見誤っていました。 型表記への理解が甘いようです。 確認しました。 template<class T> struct VT { static const int type = 1;}; template<class T,int N> struct VT< T[N] > { static const int type = 2;}; template<class T,int N> struct VT< const T[N] > { static const int type = 4;}; template<class T> struct VT< T* > { static const int type = 3;}; template<class T> struct VT< T& > { static const int type = 9;}; template<class T> struct VT< T*&& > { static const int type = 7;}; template<class T> struct VT< T*& > { static const int type = 8;}; template<class T,int N> struct VT< T(&&)[N] > { static const int type = 10;}; template<class T,int N> struct VT< T(&)[N] > { static const int type = 11;}; #include<iostream> int main(){ // 11 std::cout << VT< decltype("") >::type << std::endl; }