c++ - Clang、MSVC、GCC 不同意使用 index_sequence 中的函数应用程序编译此代码
问题描述
此代码来自另一个问题的答案:
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++ 标准中是模糊的还是只是一个实现错误?我看不出为什么在这个地方不允许函数调用,对于 a 的构造也是如此F{}
。
解决方案
- 此代码来自김선달上面的评论:
#include <utility>
#include <type_traits>
template<typename F, typename Seq>
struct apply_impl;
template<typename F, std::size_t... Is>
struct apply_impl<F, std::index_sequence<Is...>> {
private:
static constexpr auto test(F, std::index_sequence<Is...>)
-> decltype(std::index_sequence< F{}(Is)... > {});
public:
using type = decltype(test(F{}, std::index_sequence<Is...>{}));
};
template<typename F, typename Seq>
using apply = typename apply_impl<F, Seq>::type;
constexpr auto f = [](auto x) -> std::size_t { return x*x; };
using res_type = apply<decltype(f), std::index_sequence<0, 1, 2, 3, 4> >;
static_assert(std::is_same<res_type, std::index_sequence<0, 1, 4, 9, 16>>::value);
注意:提供解决方案作为答案而不是评论。
推荐阅读
- reactjs - 具有完整类型检查的 React Typescript Context API HOC
- slurm - Slurm将单个节点拆分为多个
- php - 如何专门将此 php 代码转换为 html?
- php - 使用 URI 或路由器加密 URI 字符串 Codeigniter
- api - Symfony 4 JWT:访问 api 时我无法从控制器获得响应
- r - 正则表达式搜索表达式使用 r 中的官员查找分块的单词文本
- java - Tomcat无法连接到8005端口
- sql - sql server中两个时间列之间的时间差
- angular - 订阅 rxjs observable 时出现“您提供了无效对象”错误
- java - 有没有办法在 Eclipse Project Explorer 的 Maven 依赖项中获取版本信息而不是“(没有测试代码)”?