模板模板参数和默认参数 [英] Template template parameters and default arguments
问题描述
考虑以下代码,它使用模板模板"参数来实例化使用多种类型的类模板:
Consider the following code which uses "template template" parameters to instantiate a class template using multiple types:
#include <iostream>
using namespace std;
enum E
{
a = 0,
b = 1
};
template <template <E> class Action, class T>
void do_something(const T& value)
{
typedef Action<a> type1;
typedef Action<b> type2;
}
template <E e, class Enable = void>
class Foo
{
};
int main()
{
do_something<Foo>(int(55));
}
使用较旧的编译器(GCC 4.1.2),上面的代码编译得很好.但是,使用较新的编译器(GCC 4.4.6 或 4.8.1),会产生以下错误:
Using an older compiler (GCC 4.1.2), the above code compiles fine. However, using a newer compiler (GCC 4.4.6 or 4.8.1), the following error is produced:
test3.cpp:25:27: error: no matching function for call to ‘do_something(int)’
do_something<Foo>(int(55));
所以看起来GCC不能绑定do_something
,因为模板模板参数只声明了一个参数(一个Enum),但是Foo
实际上需要两个模板参数(即使一个是默认的.)我猜 GCC 4.1.2 允许忽略默认参数.
So it looks like GCC can't bind to do_something
, because the template template parameters only declare a single parameter (an Enum), but Foo
actually takes two template parameters (even though one is default.) I guess GCC 4.1.2 allowed the default parameter to be ignored.
好的,如果我将模板定义更改为:
Okay, so if I change the template definition to:
template <template <E, class> class Action, class T>
void do_something(const T& value)
{
typedef Action<a> type1;
typedef Action<b> type2;
}
...那么我测试过的 GCC 版本都不会编译它.它们都产生了类似的错误:
...then no version of GCC I tested will compile it. They all produce a similar error:
test3.cpp:13: error: wrong number of template arguments (1, should be 2)
test3.cpp:10: error: provided for ‘template<E <anonymous>, class> class Action’
所以现在,编译器抱怨是因为表达式 typedef Actiontype1
只提供一个模板参数.显然,我无法在这里隐式使用默认参数.
So now, the compiler complains because the expression typedef Action<a> type1
only provides a single template parameter. Apparently, I'm not able to implicitly use the default parameter here.
有什么方法可以在模板模板函数中使用模板的默认参数吗?
Is there some way I can use the default parameter of a template in a template template function?
推荐答案
模板参数的参数将忽略默认参数.在n3337,章节[temp.arg.template]
,第2段中有这个例子:
Default arguments are ignored for parameters of template arguments. There's this example in n3337, chapter [temp.arg.template]
, paragraph 2:
template<class T> class A { /∗ ... ∗/ };
template<class T, class U = T> class B { /∗ ... ∗/ };
template <class ... Types> class C { /∗ ... ∗/ };
template<template<class> class P> class X { /∗ ... ∗/ };
template<template<class ...> class Q> class Y { /∗ ... ∗/ };
X<A> xa; // OK
X<B> xb; // ill-formed: default arguments for the parameters of a template argument are ignored
X<C> xc; // ill-formed: a template parameter pack does not match a template parameter
Y<A> ya; // OK
Y<B> yb; // OK
Y<C> yc; // OK
注意 X 处的注释xb;
以上.恐怕我找不到规范文本.
Note the comment at X<B> xb;
above. I can't find the normative text, I'm afraid.
您可以将其与函数相关联——默认参数也不是签名的一部分.如果您尝试调用具有通过函数指针默认参数的函数,也会发生同样的事情.
You can correlate this with functions - default arguments are not a part of a signature, either. The same thing would also happen if you tried to call a function that has a parameter defaulted through a function pointer.
这篇关于模板模板参数和默认参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!