【问题标题】:C++ 17: Using alias template bug in gcc?C++ 17:在 gcc 中使用别名模板错误?
【发布时间】:2021-03-12 17:21:50
【问题描述】:

片段:


#include <functional>

template <typename T>
struct CallableTrait;

template <typename R, typename... Args>
struct CallableTrait<std::function<R(Args...)>>
{
    using ReturnType = R;
};

template <typename Callable>
using CallableTraitT = CallableTrait<decltype(std::function{std::declval<Callable>()})>;

template <typename Callable>
auto test(Callable&&)
{
    using CallableInfo = CallableTraitT<Callable>;
    static_assert(!std::is_void_v<typename CallableInfo::ReturnType>);
}

int main()
{
    test([]() { return 42; });
    return 0;
}

Demo

这与clang-12.0.0MSVC-19.16.27034 编译得很好,但是gcc-11.0.0 会抛出一个错误:

prog.cc: In instantiation of 'auto test(Callable&&) [with Callable = main()::<lambda()>]':
prog.cc:25:29:   required from here
prog.cc:20:25: error: invalid use of incomplete type 'struct CallableTrait<main()::<lambda()> >'
   20 |     static_assert(!std::is_void_v<typename CallableInfo::ReturnType>);
      |                    ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
prog.cc:5:8: note: declaration of 'struct CallableTrait<main()::<lambda()> >'
    5 | struct CallableTrait;
      |    

谁对谁错?

编辑: 此处跟踪错误gcc-bugzilla

【问题讨论】:

  • []() { return 42; }std::function&lt;int()&gt;.
  • 你想要的基本上就是我在这里做的:stackoverflow.com/a/53673648/4342498
  • @NathanOliver:谢谢,但问题是为什么 3 个编译器中有 2 个是好的,而 gcc 认为这是错误的。
  • @Eljay:但CallableTraitT“呼叫”std::function...
  • 确实,添加std::function 让gcc 很高兴Demo

标签: c++ templates c++17 language-lawyer


【解决方案1】:

在此处跟踪问题gcc-bugzilla

【讨论】:

  • 我不这么认为。乔纳森只是简化了示例,尚未正式确认。
  • @cigien:我的错。我不知道 bugzilla 中的语义。将进行防御性编辑。
  • 不用担心。感谢您提交错误报告。它确实对我来说看起来像一个错误,因为它值得:)
猜你喜欢
  • 2020-06-02
  • 2018-10-12
  • 1970-01-01
  • 1970-01-01
  • 2017-05-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多