【发布时间】:2010-12-02 02:09:30
【问题描述】:
我在 g++ 4.4 和 4.5 上发现了一个奇怪的问题。我问过这个问题是因为我认为我在代码中犯了一些愚蠢的错误。原始帖子是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)
这似乎不是真正的程序员错误,尽管我可能错过了模板模板参数解析的一些模糊规则。但是我尝试将错误发布到 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.
【问题讨论】:
-
我删除了盗版 C++ 标准的链接。如果 ISO 发现你链接到它的盗版副本,我不知道他们会对你做什么。最好告诉人们用谷歌搜索“ISO IEC 14882 2003 C++”,以便在 ISO 目录中找到它,然后...... :)
-
顺便说一句,Clang 可以很好地编译此代码。而且我没有看到规范说这是无效的(我也不明白为什么它应该是无效的,但是我从未尝试过编写 C++ 编译器)。所以我认为这是有效的。
-
难以置信,我不知道你可以“盗版”iso c++ 标准。不公开的标准有什么意义?荒谬! - 是的,我尝试过 clang,它也适用于我,所以我认为这只是一个 g++ 错误
-
所以我收回了,也许不是错误; g++ 和英特尔编译器都同意不接受此代码的几率是多少,除非英特尔编译器团队只是密切关注 g++ 所做的事情?
-
comeau online 也同意他们俩,所以出于某种原因,标准(无论是 ISO 还是事实上的)似乎是这个代码是错误的,即使我们不能确切地说明原因跨度>
标签: c++ g++ compiler-errors standards-compliance template-templates