【问题标题】:Is it possible to transform the types in a parameter pack?是否可以转换参数包中的类型?
【发布时间】:2011-03-17 10:54:38
【问题描述】:

是否可以转换参数包的类型并传递它?

例如给定以下内容:

template<class... Args> struct X {};
template<class T> struct make_pointer     { typedef T* type; };
template<class T> struct make_pointer<T*> { typedef T* type; };

我们能否定义一个模板 magic 或类似的东西,以便以下断言成立:

typedef magic<X, make_pointer, int, char>::type A;
typedef X<int*, char*> B;
static_assert(is_same<A, B>::value, ":(");

【问题讨论】:

    标签: c++ templates metaprogramming c++11


    【解决方案1】:

    是的,我们可以做到这一点

    template<template<typename...> class List, 
             template<typename> class Mod, 
             typename ...Args>
    struct magic {
        typedef List<typename Mod<Args>::type...> type;
    };
    

    【讨论】:

    • 啊,§14.5.3/5 甚至包括我设法忽略的示例:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-29
    • 1970-01-01
    • 2016-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多