それはextern template declaration
とexplicit template instantiation
に来るときtypedef
は私を混乱させる2つのケースがあります。テンプレートのインスタンスとのexternテンプレート宣言にtypedefを使用して
2つの例を示すには、次の2つのコードスニペットを参照してください。
は例(ケース1)次の点を考慮
// suppose following code in some cpp file
template <typename T>
struct example
{
T value;
};
// valid typedefs
typedef example<int> int_example;
typedef example<std::string> string_example;
// explicit instantiation using above typedefs
template class int_example; // -> compile time error
template class string_example; // -> compile time error
// instead we need to use type names
template class example<int>; // -> OK
template class example<std::string>; // -> OK
// QUESTION 1: Why does this work however? is this valid code?
typedef std::string type_string;
template class example<type_string>;
なぜtypedefを持つtemplate class example<type_string>
仕事?なぜtemplate class string_example
は有効ではありませんか?
次の例(ケース2)考えてみましょう:上記のコメントに疑問として、それは上記の例のように、extern template declaration
にtypedefを使用して有効であり、そしてなぜこれがとは違ってコンパイルん
// suppose following code is in some header file
template <typename T>
struct example
{
T value;
};
// valid typedefs
typedef std::string type_string;
typedef example<type_string> string_example;
// Explicit instantiation declaration
// QUESTION 2: Is this valid code? if not why not?
extern template string_example; // -> at least this compiles, but is it OK?
をケース1ここではありません。
私は同様のケースについて読んだことがありますが、上記の2つの質問に対する詳細な答えはありません。詳細な精緻化が非常に高く評価されています!
あなたはなぜ 'テンプレートクラスの例を知っていますか;'と 'のexternテンプレートクラスの例;'は動作します(またはそれがGCCでない)、私はところでMSVC-140を使用していますし、それが動作します。本当にありがとう! –
codekiddy
'example'は* simple-template-id *とみなされます。 –
GCCでいくつかのテストを行いましたが、g ++が 'example'のような場合に未定義の参照を発行するケースがありますが、msvcはうまくコンパイルされますが、msvcが警告を出すがg ++ではない場合もあります。どちらもスタンダードによって記載されておらず、よく定義さ –
codekiddy