2012-02-07 12 views
7

これはなぜ機能しますか?const型変数を非型テンプレートパラメータ(VARIABLEは定数式には現れません)

char __nontype[] = "foo"; 
typedef TemplateClass<T, __nontype> MyClass; 

ただし、これは(定数変数を使用して)ですか?

const char __nontype[] = "foo"; 
typedef TemplateClass<T, __nontype> MyClass; 

コンパイラエラー:

エラー: '__nontypeは' 定数式に表示することはできません

エラー:結果がありません:テンプレート引数2は

+2

これは、TemplateClass <>の定義によって異なる場合があります。投稿できますか? – hatboyzero

+2

@hatboyzeroそれはありません – sehe

+0

ここでの基本的な誤解は、あなたが 'const'修飾子が自動的に何かを' constexpr'にすると仮定していると思います。文字配列はコンパイル時定数ではありません。彼らはリンク時まで知られていません。 – tenfour

答えて

5

差である:ここ

希望ワークがその例です。 externを追加すると動作します。つまり、私が言うことができる限り、

14.3.2 Template non-type arguments [temp.arg.nontype]

A template-argument for a non-type, non-template template-parameter shall be one of:

  • an integral constant expression (including a constant expression of literal class type that can be used as an integral constant expression as described in 5.19); or
  • the name of a non-type template-parameter; or
  • a constant expression (5.19) that designates the address of an object with static storage duration and external or internal linkage or a function with external or internal linkage, including function templates and function template-ids but excluding non-static class members, expressed (ignoring parentheses) as & id-expression, except that the & may be omitted if the name refers to a function or array and shall be omitted if the corresponding template-parameter is a reference; or
  • a constant expression that evaluates to a null pointer value (4.10); or
  • a constant expression that evaluates to a null member pointer value (4.11); or
  • a pointer to member expressed as described in 5.3.1.

externなしでも動作するはずです。オブジェクトは内部リンケージを持つことができますが、コンパイラはそれをまだサポートしていません。これはC++ 11の変更の1つです。以前のC++標準では許可されていませんでした。

+1

これは問題だと思われます。良い答え!今私が気づいた限り、内部リンケージを可能にすることは新しいです(C++ 11?)。しかし、なぜそれは許されなかったのですか? – ejoerns

+1

@ejoernsこれがすべての理由であるかどうかはわかりませんが、外部リンケージでオブジェクトと関数のみを許可すると、異なるテンプレートインスタンス化のすべての名前が異なることを確実にすることが容易になります。 – hvd

+1

@ejoernsあなたはC++ 11の新機能だと思います。私はちょうどチェックした。 – hvd

1

エラーがそれを言う無効です。定数式(リンク時には知られていますが、コンパイル時には知られていません)。 constが結合に影響を与えるため

typedef const char *nontype_t; 
template <nontype_t> struct Y {}; 

char hello[] = "hello"; 
constexpr char* world = hello; 

int main() 
{ 
    Y<hello> a; 
} 
+0

私はそれを取得しません。ポインタへのポインタは、ポインタへのポインタよりもオブジェクトへのポインタがどのようになっていますか? – hvd

+1

次に、どのような種類のものを扱うことができる標準の部分を引用したのはなぜですか? – hvd

+0

スタティックな保存期間を持つオブジェクトで、テンプレートパラメータのタイプによっては有効です。この問題は、パラメータの型が非const char型の配列である可能性があります。 –

関連する問題