简体   繁体   中英

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM