2
int max(int n, int ... rest)
{
    //Do stuff
}

Why is something like this not allowed? If you want to create a function that accepts variadic function arguments, you are forced to use templates and typenames. However, even if you know that you will only accept arguments of a certain type (int in this case), why does C++ force you to do

template <typename ... N>
int max(int n, N ... rest)
{
    //Do stuff
}
Constructor
  • 7,273
  • 2
  • 24
  • 66
1110101001
  • 4,662
  • 7
  • 26
  • 48
  • Function parameters are not (considered to be) constant expressions. – dyp Jun 11 '14 at 17:29
  • Related: http://stackoverflow.com/questions/3703658/specifying-one-type-for-all-arguments-passed-to-variadic-function-or-variadic-te and http://stackoverflow.com/q/17792051/420683 and http://stackoverflow.com/q/9843567/420683 and http://stackoverflow.com/q/18017543/420683 (looks like a lot of dups to me, this list -- the OP not included) – dyp Jun 11 '14 at 17:30
  • @dyp The linked question doesn't have any explanations as to why – 1110101001 Jun 11 '14 at 17:32
  • I know, that's why I didn't mark this as a duplicate. I post related questions for others who might find this question and are looking for a workaround, for example. – dyp Jun 11 '14 at 17:33
  • What if you leave `rest` nameless? Then it's just `int max(int n, int...)`, which would be ambiguous. – chris Jun 11 '14 at 17:39
  • This is probably solved with [constraints](http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n3889.pdf) (the example in 6.1/20 indicates it IMHO) -- though it'll probably have different semantics (regarding implicit conversions) – dyp Jun 11 '14 at 17:39
  • Is your question really just "why"? What kind of answer do you expect? "Because it's not in the language." – Kerrek SB Jun 11 '14 at 17:52
  • 1
    **close-vote**: *primarily opinion-based*. Related proposal: [Fixed Size Parameter Packs - Proposal](https://groups.google.com/a/isocpp.org/d/msg/std-proposals/igOzjW-5KLM/hMo2GE-HTccJ) – Filip Roséen - refp Jun 11 '14 at 17:58

0 Answers0