4

g++ 4.4 および 4.5 で奇妙な問題を発見しました。コードでばかげたエラーを起こしていると思ったので、これについて尋ねました。元の投稿はここにありますが、投稿を完了するために、問題のある問題のあるコードをここに再投稿します。

$ cat templatetemplate.cc
template <int i>
struct LabelTypeMap { typedef int type_t; };

template <bool>
struct Hold { typedef int type; };

template<typename Holder, template<typename Holder::type> class typeMap>
struct Whatever { };

template <bool Enable>
struct Now { typedef Whatever<Hold<ENABLE>, LabelTypeMap> concrete_t; };

Now<true>::concrete_t obj;

$ g++ -DENABLE=Enable -c templatetemplate.cc
templatetemplate.cc:11: error: type/value mismatch at argument 2 in template parameter list for ‘template<class Holder, template<typename Holder::type <anonymous> > class typeMap> struct Whatever’
templatetemplate.cc:11: error:   expected a template of type ↵
    ‘template<typename Holder::type <anonymous> > class typeMap’, got ↵
    ‘template<int i> struct LabelTypeMap’
marcelo@macbookpro-1:~/play$ 
$ g++ -DENABLE=true -c templatetemplate.cc
(no error)

これは実際にはプログラマーのエラーではないように思われますが、テンプレートのテンプレート パラメーター解決に関するあいまいなルールが欠落している可能性はあるかもしれません。ただし、バグをubuntuトラッカーに投稿しようとしました(うまくいけば、バグを却下するか、バグを上流に送信します)

それで、これが本当にバグかどうかを確認するためだけに、私は 2003 年標準のコピーを手に入れました。セクション 14.3.3 を数回読みましたが、合格したかどうかのわずかな手がかりを見落としていると感じています。サンプル コードのようなパラメーターを持つテンプレート テンプレート パラメーターは許可または禁止されます。ドキュメントのこの部分がこれについて何か言及しているかどうかさえわかりません

これが私の質問です。これがどこに指定されているか知っていますか?

編集: この質問が 1 週間以上回答されていないことは非常に興味深いことです: 以前のテンプレート パラメーターを使用して後続のテンプレート パラメーターの型を指定できるかどうか (少なくともそれは基本的に実装者の決定に委ねられています

2 番目の編集 (2011 年 10 月 1 日): 人々、これにはおそらく私たち全員が見逃している何かがあります (または、多くの熟練したコンパイラ設計者が間違っています): Intel C++ コンパイラ XE 12.0 でこれを試したところ、次のようになりました:

 $icpc ttemplatetemplate.cc -o ./x2test 
templatetemplate.cc(12): error: class template "LabelTypeMap" is not compatible with template template parameter "typeMap"
  struct Now { typedef Whatever<Hold<Enable>, LabelTypeMap> concrete_t; };
                                              ^


 compilation aborted for templatetemplate.cc (code 2)
$ icpc --version
icpc (ICC) 12.0.0 20101116
Copyright (C) 1985-2010 Intel Corporation.  All rights reserved.
4

4 に答える 4

1

Comeauでこの単純なコード(私にはあなたの問題の単純化のようです)を試しましたが、それを禁止する標準には何も見つかりません:

template<int>
class A {};

template<class T, template<T> class U>
class B {};

B<int, A> b;

そして、次のエラーが発生します。

"ComeauTest.c"、4 行目: エラー:テンプレート テンプレート パラメーターのパラメーターは、別のテンプレート パラメーターの型に依存することはできません

標準のどの部分が実際に禁止されているかを見つけられたらいいのに...

于 2010-12-02T08:24:03.967 に答える
1

ところで、根本的な問題に対して私が見つけた解決策は次のとおりです。

template <int i>
struct LabelTypeMap { typedef int type_t; };

template <bool>
struct Hold { typedef int type; };

template<typename Holder>
struct Whatever {
  typedef typename Holder::type HT;
  template <template <HT> class typeMap>
  struct Whatever2 { };
};

template <bool Enable>
struct Now { typedef typename Whatever<Hold<Enable> >::Whatever2<LabelTypeMap> concrete_t; };

ネストされたテンプレートを使用して、typename経由でを導入できtypedefます。

于 2010-12-02T16:24:34.987 に答える
0

私はこの奇妙な構造を疑っtemplate<typename Holder::type> class typeMap ています: それは何ですか? typename Holder::type少し奇妙です。これはダミーの名前 (識別子) であるはずです。Holder::type識別子でさえありません。

于 2010-12-02T12:55:03.033 に答える
-1

これが本来あるべき姿ではないでしょうか。

これは、LabelTypeMap 自体がテンプレート (テンプレート テンプレート パラメーター) であるため、型を指定する必要があるためです。

template <bool Enable> 
struct Now { typedef Whatever<Hold<ENABLE>, LabelTypeMap<ENABLE> > concrete_t; };
于 2010-12-02T08:41:37.397 に答える