Home > Mobile >  Unable to compile constexpr function with variadic arguments
Unable to compile constexpr function with variadic arguments

Time:10-09

I'm writing a constexpr product function with variadic arguments. I have only been able to get "Version 1" working below. When attempting to compile "Version 2", I get the error Declaration type contains unexpanded parameter pack 'data_type'. Can anyone explain why this is the case?

/** Version 1. */
template <typename data_type, typename ...data_types>
constexpr data_type Product1(data_type _first, data_types ..._rest) // This code compiles :)
{
  return _first*(_rest * ...);
}

/** Version 2. */
template <typename ...data_type>
constexpr data_type Product2(data_type ..._rest) // This code does not compile :(
{
  return (_rest * ...);
}

CodePudding user response:

Change the return type to auto, datatype is a pack and cannot be used as return type

template <typename ...data_type>
constexpr auto Product2(const data_type& ..._rest) 
{
    return (_rest * ...);
}

int main()
{
    constexpr auto p = Product2(1, 2, 3);
    return 0;
}
  • Related