Can parameter pack function arguments be defaulted?

后端 未结 2 948
生来不讨喜
生来不讨喜 2020-12-09 15:54

This is a point about which gcc 4.9.2 and clang 3.5.2 are in sharp disagreement. The program:

template
int foo(int i = 0, Ts &&         


        
相关标签:
2条回答
  • 2020-12-09 15:59

    A Kerrek SB says, it's not possible. What you could do, instead, is using a std::tuple

    template <class ... Args>
    void foo( std::tuple<Args...> t = std::tuple<int>(0) )
    {}
    
    0 讨论(0)
  • 2020-12-09 16:10

    From 8.3.6 ([dcl.fct.default])/3:

    A default argument shall not be specified for a parameter pack.

    From 8.3.6 ([dcl.fct.default])/4:

    In a given function declaration, each parameter subsequent to a parameter with a default argument shall have a default argument supplied in this or a previous declaration or shall be a function parameter pack.

    So this allows code like void f(int a = 10, Args ... args), or indeed like your first snippet. (Thanks to @T.C. for looking up the second sentence!)

    0 讨论(0)
提交回复
热议问题