Home > Net >  Abbreviated function template syntax for user-defined class template argument deduction guide
Abbreviated function template syntax for user-defined class template argument deduction guide

Time:12-15

I'm writing a deduction guide in the style of an abbreviated function template, but I'm not sure if it's allowed. It compiles on gcc and clang, but not msvc.

The error is:

error C3539: a template-argument cannot be a type that contains 'auto'

Which compiler is doing the right thing?

try it out here

template <typename Type, int Count>
struct Array
{
    Type data[Count];

    Array (auto ... arg)
    {
        int count = 0;
        ((data[count  ] = arg),...);
    }
};

// abbreviated function template syntax - fails in msvc
Array (auto first, auto ... next) -> Array<decltype(first), 1   sizeof...(next)>;

// regular syntax
// template <typename Type, typename ... Args> Array (Type first, Args ... next) -> Array<Type, 1   sizeof...(Args)>;

int main ()
{
    Array a(1,2,3);
}

CodePudding user response:

According to [dcl.spec.auto.general]/6 any use of a placeholder type (e.g. auto) that is not explicitly permitted in [dcl.spec.auto] is not permitted.

And I don't see anything applying to deduction guides mentioned there. In particular [dcl.spec.auto.general]/2 which allows it in function parameters is explicitly limited to parameter-declarations in function declarations and lambda expressions.

So it does seem ill-formed to me.

However, I don't see any reason for this. I suspect that [dcl.spec.auto] and the definition of abbreviated function template in [dcl.fct] should just be extended to include parameter lists of deduction guides with analogues rules to match the statement in [temp.deduct.guide] that the parameter list of a deduction guide follows the same restrictions as that of a function declaration.

  • Related