Variadic templates

前端 未结 8 1893
野趣味
野趣味 2020-12-06 00:05

I have seen a lot of links introducing the variadic templates. But I have never seen any compilable example that demonstrates this approach.

Could someone provide me

相关标签:
8条回答
  • 2020-12-06 00:50

    This is an example of variadic templates that I put up on my blog: http://thenewcpp.wordpress.com/2011/11/23/variadic-templates-part-1-2/

    It compiles. It demonstrates finding the largest type from a group of types.

    #include <type_traits>
    
    template <typename... Args>
    struct find_biggest;
    
    //the biggest of one thing is that one thing
    template <typename First>
    struct find_biggest<First>
    {
      typedef First type;
    };
    
    //the biggest of everything in Args and First
    template <typename First, typename... Args>
    struct find_biggest<First, Args...>
    {
      typedef typename find_biggest<Args...>::type next;
      typedef typename std::conditional
      <
        sizeof(First) >= sizeof(next),
        First,
        next
      >::type type;
    };
    
    0 讨论(0)
  • 2020-12-06 00:50

    another syntax: expanding, e.g.

    template<typename VAL, typename... KEYS>
    class MyMaps
    {
      typedef std::tuple< std::map<KEYS,VAL>... > Maps;
    }
    

    hence:

    MyMaps<int,int,string>:Maps
    

    is now actually:

    std::tuple<std::map<int,int>,std::map<string,int> >
    
    0 讨论(0)
提交回复
热议问题