我在 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.