我在 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 tracker(希望他们会忽略它或以其他方式将错误发送到上游)
所以,为了检查这是否真的是一个错误,我给自己弄了一份 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.
最佳答案
虽然我在 Comeau 中尝试了这个简单的代码(在我看来是对您的问题的简化),但我在标准中找不到任何禁止它的内容。 :
template<int>
class A {};
template<class T, template<T> class U>
class B {};
B<int, A> b;
它会产生以下错误:
"ComeauTest.c", line 4: error: a parameter of a template template parameter cannot depend on the type of another template parameter
我希望我能找到标准的哪一部分实际上禁止它...
关于c++ - 是否应允许模板模板参数接收从先前参数解析的参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4331027/