【发布时间】:2021-11-27 23:42:18
【问题描述】:
这段代码是从answer到另一个问题:
template <typename F, std::size_t ... Is>
constexpr auto apply(F f, std::index_sequence<Is...>)
-> std::index_sequence<f(Is)...>
{
return {};
}
gcc 失败
<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 {};
}
<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{} 的构造也是如此。
【问题讨论】:
-
@xskxzr 你需要
#include <cstddef> #include <utility> -
如果你想让它工作:godbolt.org/z/dz5rdKnsT
-
考虑到
f(Is)是模板参数列表中的模板参数,根据temp.variadic#5.7,这应该是一个有效的包扩展。 -
clang 怎么样?
标签: c++ g++ language-lawyer c++20 clang++