Clang、MSVC、GCC 不同意在 index_sequence 中使用函数应用程序来编译此代码

NoS*_*tAl 7 c++ g++ language-lawyer clang++ c++20

这段代码来自另一个问题的回答:

template <typename F, std::size_t ... Is>
constexpr auto apply(F f, std::index_sequence<Is...>)
-> std::index_sequence<f(Is)...>
{
    return {};
}
Run Code Online (Sandbox Code Playgroud)

gcc 失败

<source>:5:29: error: expected parameter pack before '...'
Run Code Online (Sandbox Code Playgroud)

msvc 和 clang 编译它。

现在将其更改为此会导致 msvc 失败:

template <typename F, std::size_t ... Is>
constexpr auto apply(F , std::index_sequence<Is...>)
-> std::index_sequence<F{}(Is)...>
{
    return {};
}
Run Code Online (Sandbox Code Playgroud)
<source>(5): error C2187: syntax error: '<end Parse>' was unexpected here 
<source>(5): error C2059: syntax error: '('
<source>(6): error C2988: unrecognizable template declaration/definition 
<source>(6): error C2059: syntax error: '{'
<source>(6): error C2143: syntax error: missing ';' before '{''
<source>(6): error C2447: '{': missing function header (old-style formal list?)
Run Code Online (Sandbox Code Playgroud)

这是 C++ 标准中含糊不清的内容还是只是一个实现错误?我看不出为什么在这个地方不允许函数调用,对于构建F{}.

小智 0

    \n
  1. 此代码来自\xea\xb9\x80\xec\x84\xa0\xeb\x8b\xac \ 上面的评论:
  2. \n
\n
#include <utility>\n#include <type_traits>\n\ntemplate<typename F, typename Seq>\nstruct apply_impl;\n\ntemplate<typename F, std::size_t... Is>\nstruct apply_impl<F, std::index_sequence<Is...>> {\n  private:\n    static constexpr auto test(F, std::index_sequence<Is...>)\n    -> decltype(std::index_sequence< F{}(Is)... > {});\n  public:\n    using type = decltype(test(F{}, std::index_sequence<Is...>{}));\n};\n\ntemplate<typename F, typename Seq>\nusing apply = typename apply_impl<F, Seq>::type;\n\nconstexpr auto f = [](auto x) -> std::size_t { return x*x; };\n\nusing res_type = apply<decltype(f), std::index_sequence<0, 1, 2, 3, 4> >;\n\nstatic_assert(std::is_same<res_type, std::index_sequence<0, 1, 4, 9, 16>>::value);\n
Run Code Online (Sandbox Code Playgroud)\n
    \n
  1. 这是来自Mechap上面的评论

    \n

    在 a 中,template-argument-list ([temp.arg]);模式是 a template-argument。来自文档

    \n
  2. \n
\n

注意:提供解决方案作为答案而不是评论。

\n