我在 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++ 标准没有指定我们是否可以使用先前的模板参数来指定后续模板参数的类型(至少在规定的形式),这基本上留给实现者来决定
第二次编辑(10/01/2011):人们,可能有一些我们都遗漏的东西(否则很多高技能的编译器设计者都是错误的):
我用 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.
I've found a weird issue on g++ 4.4 and 4.5. I've asked about this because i thought that i was making some silly error in the code. The original post is here but for post completitude i'll repost the problematic code in question here:
$ 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)
It does not seem to be really a programmer error, althought it might be possible i'm missing some obscure rule of the template template parameter resolution. However i tried posting the bug to ubuntu tracker (hopefully they'll dismiss it or otherwise send the bug upstream)
So, just for the sake of checking if this is really a bug, i got myself a copy of the 2003 standard, and i have read section 14.3.3 a couple of times now, and still i feel i miss the slightest clue if passing a template template parameter with a parameter as in the sample code is allowed or disallowed. I'm not even sure this part of the document is mentioning anything about this
Here goes my question: do you know where this is specified?
EDIT: it is pretty interesting that this question has gone unanswered for over a week now: It leads me to believe that ISO c++ standard does not specify if we can use a previous template parameter to specify types of subsequent template parameters (at least in the stated form) and that is basically left for implementers to decide
2nd EDIT (10/01/2011): People, there is probably something about this that we are all missing (or else lots of highly skilled compiler designers are wrong):
I tried this with intel c++ compiler XE 12.0 and i got this:
$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)
我在标准中找不到任何禁止它的内容,尽管我在 Comeau:
它会产生以下错误:
我希望我能找到标准的哪一部分实际上禁止它......
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 :
And it produces the following error :
I wish I could find which part of the standard actually forbids it...
顺便说一句,这是我找到的根本问题的解决方案:
使用嵌套模板,我可以通过
typedef
引入typename
。BTW, here's the solution I found to the underlying problem:
Using a nested template, I can introduce a
typename
via atypedef
.我怀疑这个奇怪的构造:
template类类型映射
那应该是什么?
typename Holder::type
位很奇怪;这应该是一个虚拟名称(标识符)。Holder::type
甚至不是标识符。I'm suspecting this weird construct:
template<typename Holder::type> class typeMap
What's that supposed to be? The
typename Holder::type
bit is weird; that's supposed to be a dummy name (identifier).Holder::type
isn't even an identifier.这不是应该的样子吗?
这是因为 LabelTypeMap 本身就是一个模板(模板模板参数),因此需要指定类型。
Isn't this how it is supposed to be?
This is because LabelTypeMap is itself a template (template template parameter) and hence requires a type to be specified.