Variadic templates

前端 未结 8 1923
野趣味
野趣味 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 
    
    template 
    struct find_biggest;
    
    //the biggest of one thing is that one thing
    template 
    struct find_biggest
    {
      typedef First type;
    };
    
    //the biggest of everything in Args and First
    template 
    struct find_biggest
    {
      typedef typename find_biggest::type next;
      typedef typename std::conditional
      <
        sizeof(First) >= sizeof(next),
        First,
        next
      >::type type;
    };
    

提交回复
热议问题