Variadic templates and switch statement?

后端 未结 5 1354
栀梦
栀梦 2020-12-24 13:42

I have the following function which can take N arguments of different types, and forwards them to N functions templated on each individual type, in this manner (example with

5条回答
  •  忘掉有多难
    2020-12-24 14:18

    Just for fun, I propose the following way

    template 
    bool func (int & cnt, Ts ... xs)
     {
       using unused = int[];
    
       int  i   { -1 };
       bool ret { true };
    
       (void)unused { 0, ((++i == cnt ? (ret = func(xs)) : true), 0)... };
    
       if ( ret && (cnt <= i) )
          ++cnt;
    
       return ret;
     }
    

    but I don't think is a efficient way as the switch way.

提交回复
热议问题