Search code examples
c++gccautoperfect-forwardingc++-concepts

Perfect forwarding return type of a template class member function


Consider this code:

int TEN = 10;

template < typename >
struct XX
{
    //static auto&& ban(auto&&...) // FAILS!?
    template < typename... Args > static auto&& ban(Args&&...)
    { return TEN; }
};

int main()
{
    XX<void>::ban();
    return 0;
}

The declaration of ban(auto&&...) fails with

error: invalid initialization of reference of type 'auto&&' from expression of type 'int'

when compiling with gcc-8.3 -std=c++17 -fconcepts.

So, is this a bug in the implementation of GCC?

Note that it passes when class XX is not a template.


Solution

  • Indeed it look like a compiler bug.

    A simple workaround would be to use trailing return type:

    static auto ban(auto&&...) -> auto&& // strangely work
    { return TEN; }
    

    Also, note that this syntax is not yet fully supported by GCC. The concept terse template syntax should allow this:

    template<typename>
    concept /* bool */ test = true;
    
    auto func(test auto) -> void {}
    

    And don't quite work yet with GCC concepts implementation.