Home > Mobile >  set::set from variadic template arguments
set::set from variadic template arguments

Time:11-08

How do I create a set from arguments of variadic templated function?

template<class T>
concept Int = std::same_as<T, int>;

template<Int... Ints>
std::set<int> fun (Ints... ints)
{
    // create and return a set containing arguments to this function
}

CodePudding user response:

You can expand the pack using pack expansion as shown below:

template<Int... Ints>
std::set<int> fun (Ints... ints)
{
    return {ints...};  //use unary right fold 
}
int main()
{
    std::set<int> myset = fun(1,2,3);
}

Demo

  • Related