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 节几次,但如果通过,我仍然觉得我错过了丝毫线索允许或不允许带有示例代码中的参数的模板模板参数。我什至不确定文件的这一部分是否提到了这方面的任何内容

我的问题是:你知道这是在哪里指定的吗?

编辑:这个问题已经有一个多星期没有回答了,这很有趣:它让我相信 ISO c++ 标准没有指定我们是否可以使用以前的模板参数来指定后续模板参数的类型(至少在规定的形式),这基本上留给实施者决定

第二次编辑(2011 年 10 月 1 日):人们,可能有一些我们都缺少的东西(或者很多高技能的编译器设计者都错了):我用英特尔 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

I cannot find anything in the standard which forbids it, although I tried this simple code (which seems to me is a simplification of your problem) in Comeau :

template<int>
class A {};

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

B<int, A> b;

And it produces the following error :

"ComeauTest.c", line 4: error: a parameter of a template template parameter cannot depend on the type of another template parameter

I wish I could find which part of the standard actually forbids it...

于 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 回答