Re: Multiple parameter packs in function parameters

Discussion in 'C++' started by Victor Bazarov, Nov 30, 2012.

  1. On 11/30/2012 6:28 AM, Juha Nieminen wrote:
    > Is this valid code, or should it even compile?
    >
    > template<typename... Pack1, typename... Pack2>
    > void func(Pack1... pack1, Pack2... pack2) {}
    >
    > Currently the behavior of that seems quite erratic in different compilers.
    >
    > In gcc you can t call that with zero arguments, but you can call it with
    > at least one argument (such as "func(5);") All the parameters are passed
    > as 'pack2'.
    >
    > With clang if you try to call it directly with "func()" or "func(5)" you
    > get strange and conflicting error messages (in the first case it tells
    > that 'func' requires 1 parameter and in the second case that it requires
    > 0 parameters.) However, if you call it like "func<int>(5)" then it
    > compiles (all parameters are passed as 'pack1'.)
    >
    > But if I understand correctly, it shouldn't compile at all, no matter
    > how it's called. What does the standard say?


    According to [temp.param]/11, there can be at most one occurrence of
    template parameter pack, and it has to be the last. The wording is: "If
    a template-parameter of a primary class template or alias template is a
    template parameter pack, it shall be the last template-parameter."

    IOW, your declaration should not compile.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 30, 2012
    #1
    1. Advertising

  2. Victor Bazarov

    Zhihao Yuan Guest

    On Friday, November 30, 2012 7:41:38 AM UTC-6, Victor Bazarov wrote:
    > According to [temp.param]/11, there can be at most one occurrence of
    > template parameter pack, and it has to be the last. The wording is: "If
    > a template-parameter of a primary class template or alias template is a
    > template parameter pack, it shall be the last template-parameter."


    We are talking about the function template, not a class template or
    an alias template.

    --
    Zhihao Yuan, ID lichray
    The best way to predict the future is to invent it.
    ___________________________________________________
    4BSD -- http://4bsd.biz/
    Zhihao Yuan, Nov 30, 2012
    #2
    1. Advertising

  3. On 11/30/2012 9:02 AM, Zhihao Yuan wrote:
    > On Friday, November 30, 2012 7:41:38 AM UTC-6, Victor Bazarov wrote:
    >> According to [temp.param]/11, there can be at most one occurrence of
    >> template parameter pack, and it has to be the last. The wording is: "If
    >> a template-parameter of a primary class template or alias template is a
    >> template parameter pack, it shall be the last template-parameter."

    >
    > We are talking about the function template, not a class template or
    > an alias template.


    Yes. So, did you read the sentence following the one I quoted? Let me
    quote it for you here: "A template parameter pack of a function template
    shall not be followed by another template parameter unless that template
    parameter can be deduced or has a default argument (14.8.2)."

    So, can the second parameter pack in Juha's example (that you so
    conveniently snipped) be deduced? Or does it have a default argument?
    If not, the first pack "shall not be followed by another".

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 30, 2012
    #3
  4. Victor Bazarov

    Zhihao Yuan Guest

    On Friday, November 30, 2012 8:14:09 AM UTC-6, Victor Bazarov wrote:
    > So, can the second parameter pack in Juha's example (that you so
    > conveniently snipped) be deduced? Or does it have a default argument?


    Pack2 is (or, may be) deducible in n3337, and that's OK to me.

    It's not since n3481. Check this:
    http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2012/cwg_defects.html#1388

    --
    Zhihao Yuan, ID lichray
    The best way to predict the future is to invent it.
    ___________________________________________________
    4BSD -- http://4bsd.biz/
    Zhihao Yuan, Nov 30, 2012
    #4
  5. Victor Bazarov

    Zhihao Yuan Guest

    On Friday, November 30, 2012 8:14:09 AM UTC-6, Victor Bazarov wrote:
    > So, can the second parameter pack in Juha's example (that you so
    > conveniently snipped) be deduced? Or does it have a default argument?
    > If not, the first pack "shall not be followed by another".


    Pack2 is deducible since its function parameter pack appears at the
    end of the list. The call `func(5)' should deduce Pack2 to an int,
    and leave Pack1 zero type. Just like what GCC did.

    --
    Zhihao Yuan, ID lichray
    The best way to predict the future is to invent it.
    ___________________________________________________
    4BSD -- http://4bsd.biz/
    Zhihao Yuan, Nov 30, 2012
    #5
  6. On 11/30/2012 9:52 AM, Zhihao Yuan wrote:
    > On Friday, November 30, 2012 8:14:09 AM UTC-6, Victor Bazarov wrote:
    >> So, can the second parameter pack in Juha's example (that you so
    >> conveniently snipped) be deduced? Or does it have a default argument?

    >
    > Pack2 is (or, may be) deducible in n3337, and that's OK to me.
    >
    > It's not since n3481. Check this:
    > http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2012/cwg_defects.html#1388


    I am not sure what your point is. Two parameter packs in a row is not
    valid code, the arguments are not deducible no matter how you slice it.
    Or do you think otherwise? The code in the defect report has an
    intervening function argument that *could* help deducing the types in
    the pack. But in Juha's example there ain't anything that could help.
    If you think otherwise, please provide your reasoning.

    And those are moot points anyway since they concern drafts and not the
    current Standard. I thought Juha asked what the Standard said.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 30, 2012
    #6
  7. On 11/30/2012 10:12 AM, Zhihao Yuan wrote:
    > On Friday, November 30, 2012 8:14:09 AM UTC-6, Victor Bazarov wrote:
    >> So, can the second parameter pack in Juha's example (that you so
    >> conveniently snipped) be deduced? Or does it have a default argument?
    >> If not, the first pack "shall not be followed by another".

    >
    > Pack2 is deducible since its function parameter pack appears at the
    > end of the list. The call `func(5)' should deduce Pack2 to an int,
    > and leave Pack1 zero type. Just like what GCC did.


    Why shouldn't it deduce Pack1 as 'int' and leave Pack2 empty? Please
    quote the Standard to support your conclusion.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 30, 2012
    #7
  8. Victor Bazarov

    Zhihao Yuan Guest

    On Friday, November 30, 2012 9:24:34 AM UTC-6, Victor Bazarov wrote:
    > Why shouldn't it deduce Pack1 as 'int' and leave Pack2 empty? Please
    > quote the Standard to support your conclusion.


    Short Answer: Because Pack1 is not deducible, as I quoted.

    Longer one:

    To the call `func(5)',

    Before type deduction is performed, partial ordering need to be
    performed to prepare the transformed function templates
    (14.5.6.2/3, /4). But, for all transformed function templates
    with non-empty arguments with type Pack1, type deductions will fail
    because of (14.8.2.1/1).

    func(Pack1[0], Pack2...);
    func(Pack1[0], Pack1[1], Pack2...);
    ...

    However, for the transformed function templates with no arguments
    with type Pack1, Pack2 can be deduced to an `int'.

    func(Pack2[0]);

    Theoretically partial ordering should try all of the possibilities,
    but variadic templates have infinite possibilities . Clang seems to
    have a tiny glitch with partial ordering on template parameter packs,
    that's my conclusion.

    My post which quoting a DR is something irrelative. Mistaken.

    --
    Zhihao Yuan, ID lichray
    The best way to predict the future is to invent it.
    ___________________________________________________
    4BSD -- http://4bsd.biz/
    Zhihao Yuan, Nov 30, 2012
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UU5ndXllbg==?=

    Error loading webform after applying service packs

    =?Utf-8?B?UU5ndXllbg==?=, Oct 21, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    325
    =?Utf-8?B?UU5ndXllbg==?=
    Oct 21, 2004
  2. Jacob H
    Replies:
    6
    Views:
    430
    Jerry McBride
    May 4, 2004
  3. Great Job
    Replies:
    0
    Views:
    416
    Great Job
    Feb 9, 2008
  4. Marc
    Replies:
    5
    Views:
    416
    Chris Ahlstrom
    Oct 17, 2010
  5. Zhihao Yuan
    Replies:
    0
    Views:
    234
    Zhihao Yuan
    Nov 30, 2012
Loading...

Share This Page