我可以输入模板模板参数吗?

Gho*_*ost 9 c++ templates

在C++库头文件中,我们有时会看到以下内容以提高类中代码的易读性:

template<typename MyExplicitelyLongTemplateParameter>
class C
{
public:
    typedef MyExplicitelyLongTemplateParameter P;

    // Use "P" and keep your sanity.
};
Run Code Online (Sandbox Code Playgroud)

我的问题是,可以用模板模板参数做同样的事吗?

template<template<typename> typename MyExplicitelyLongTemplateParameter>
class C
{
public:
    typedef /* ??? */ P;

    // Use "P" and keep your sanity.
};
Run Code Online (Sandbox Code Playgroud)

Jam*_*lis 8

你不能创建一个typedef,不,但你可以缩短名称:

template <template <typename> typename MyExplicitlyLongTemplateParameter>
class C
{
public:

    template <typename T>
    struct P 
    {
        typedef MyExplicitlyLongTemplateParameter<T> Type;
    };

    // Use "P<T>::Type" and keep your sanity.
};
Run Code Online (Sandbox Code Playgroud)


jwi*_*mar 3

在当前标准中,您不能 typedef 模板。在即将推出的新标准中,您将能够......

  • 人们如何能够在新标准中进行 typedef 呢? (4认同)