代码之家  ›  专栏  ›  技术社区  ›  Georg Fritzsche

是否可以转换参数包中的类型?

  •  13
  • Georg Fritzsche  · 技术社区  · 16 年前

    例如,给定以下条件:

    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, ":(");
    
    1 回复  |  直到 15 年前
        1
  •  22
  •   Johannes Schaub - litb    16 年前

    是的,我们能做到

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