0

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 * ...);
}
0

1 Answer 1

2

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome, thanks so much! Order has been restored in the universe :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.