[英]How can I use template template parameters in older C++ compilers?
I have been modifying a class which contains an STL container into a template so that an allocator can be passed in.我一直在将包含 STL 容器的 class 修改为模板,以便可以传入分配器。
This goes as follows:过程如下:
class Foo
{
public:
struct Bar
{
// stuff
};
using Container = std::vector< Bar >;
private:
Container contents;
};
Becomes:变成:
template<
template <typename T> typename ALLOCATOR = std::allocator
>
class Foo
{
public:
struct Bar
{
// stuff
};
using Allocator = ALLOCATOR<Bar>;
using Container = std::vector< Bar, Allocator >;
private:
Container contents;
};
This works great on all of the platforms I have to support except one.这在我必须支持的所有平台上都很有效,除了一个。 RHEL7 uses gcc 4.8 by default and use of typename
in template type parameters seems to have been added by n4051 in gcc 5 according to this table . RHEL7 默认使用 gcc 4.8 并且在模板类型参数中使用typename
似乎已根据此表由n4051在 gcc 5 中添加。 How can this be done for older compilers?如何为较旧的编译器完成此操作?
Also I note this is a C++17 feature.我还注意到这是一个 C++17 功能。 How would these parameters be written in C++14 or earlier? C++14或者更早的这些参数怎么写呢?
The answer is in fact very simple.答案其实很简单。 Just replace typename
with class
.只需将typename
替换为class
。 That is, instead of writing this:也就是说,而不是这样写:
template<
template <typename T> typename ALLOCATOR = std::allocator
>
write this instead:改为这样写:
template<
template <class T> class ALLOCATOR = std::allocator
>
As noted in n4051 , which you quote:如您引用的n4051中所述:
This difference is artificial and is a common surprise.这种差异是人为的,是一个常见的惊喜。
There is no semantic difference between class and typename in a template-parameter type-parameter-key template-parameter type-parameter-key 中的 class 和 typename 没有语义区别
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.