【问题标题】:Perfect forwarding return type of a template class member function模板类成员函数的完美转发返回类型
【发布时间】:2019-08-14 13:21:06
【问题描述】:

考虑this代码:

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;
}

ban(auto&amp;&amp;...) 的声明因

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

使用gcc-8.3 -std=c++17 -fconcepts编译时。

那么,这是 GCC 实现中的错误吗?

请注意,当 XX 类不是模板时,它会通过。

【问题讨论】:

  • 试试-std=c++2a,不过您需要更新的 GCC 才能让它继续工作
  • @StoryTeller 也失败了。
  • @1201ProgramAlarm error: invalid initialization of reference of type 'auto&amp;&amp;' from expression of type 'int'
  • @Vahagn - 实时示例构建良好。在(取消)评论内容时,您可能链接到了错误的版本。

标签: c++ gcc auto perfect-forwarding c++-concepts


【解决方案1】:

确实它看起来像一个编译器错误。

一个简单的解决方法是使用尾随返回类型:

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

另外,请注意,GCC 尚未完全支持此语法。概念简洁模板语法应该允许这样做:

template<typename>
concept /* bool */ test = true;

auto func(test auto) -> void {}

而且还不完全适用于 GCC 概念实现。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多