Converting Variadic template pack into std::initializer_list

后端 未结 3 1813
盖世英雄少女心
盖世英雄少女心 2021-02-03 11:52

Assume that there is a function which accepts several strings:

void fun (const std::initializer_list& strings) {
  for(auto s : strings)
          


        
3条回答
  •  梦如初夏
    2021-02-03 12:20

    The second part is easier:

    template
    void foo () {
       fun({Args::value...});
    }
    

    The first part is tricky, because static_assert is a declaration, not an expression, so you'd have to expand the variadic pack within the first parameter. It may be easier just to let the call to fun do the checking for you. Here's a sketch of how to do it with an auxiliary all constexpr function:

    constexpr bool all() { return true; }
    template constexpr bool all(bool first, Args&&... rest) {
       return first && all(rest...);
    }
    
    template
    void foo () {
       static_assert(all(std::is_convertible::value...), "All Args must have a value");
       fun({Args::value...});
    }
    

提交回复
热议问题