私はそれがユーザ定義リテラルを作ることが可能だということを発見したとき、私が驚いたのテンプレート:文字列リテラルのテンプレート定義のユーザー定義リテラル(リテラルサフィックス)を作成できますか?
template <char ...C> std::string operator ""_s()
{
char arr[]{C...};
return arr;
}
// ...
std::cout << 123_s;
しかし、宣言の上には、文字列リテラルでは動作しません:
"123"_s
は私に次のエラーを与える:
prog.cpp: In function 'int main()':
prog.cpp:12:15: error: no matching function for call to 'operator""_s()'
std::cout << "123"_s;prog.cpp:4:34: note: candidate: template std::string operator""_s()
template std::string operator ""_s()prog.cpp:4:34: note: template argument deduction/substitution failed:
文字列リテラルでもテンプレート化されたユーザー定義リテラルを使用する方法はありますか?
何を達成したいですか? '' foo_s; 'を実行することはできませんので、失敗しないように引用符を付ける必要はありません。 –
@ Jean-FrançoisFabre私は知っています。もちろん、実際のユースケースではありません。私は、文字列リテラルに基づいてユニークな型を生成できるようにしたい、それは可能な方法の一つかもしれません。 – HolyBlackCat