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 失败
Run Code Online (Sandbox Code Playgroud)<source>:5:29: error: expected parameter pack before '...'
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)
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?)
这是 C++ 标准中含糊不清的内容还是只是一个实现错误?我看不出为什么在这个地方不允许函数调用,对于构建F{}
.
小智 0
#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注意:提供解决方案作为答案而不是评论。
\n