gpt4 book ai didi

c++ - MSVC : a variable with non-static storage duration cannot be used as a non-type argument

转载 作者:行者123 更新时间:2023-11-30 02:19:27 29 4
gpt4 key购买 nike

考虑以下受 this talk 启发的代码:

template<typename, typename...>
struct even_common_type_helper_impl;

template<std::size_t... Is, typename... Ts>
struct even_common_type_helper_impl<std::index_sequence<Is...>, Ts...>
{
template<std::size_t I>
using type_at = std::tuple_element_t<I, std::tuple<Ts...>>;

using even_common_type = std::common_type_t<type_at<2 * Is>...>;
};

template<typename... Ts>
using even_common_type_helper =
even_common_type_helper_impl<std::make_index_sequence<sizeof...(Ts) / 2>, Ts...>;

template<typename... Ts>
using even_common_type = typename even_common_type_helper<Ts...>::even_common_type;

基本上,我得到一个模板类型参数包,并尝试提取位于该包中偶数位置的所有类型的通用类型。

上面的代码适用于 gcc 8.1clang 6.0,但在最新的 MSVC 版本中失败并出现以下错误:

error C2971: 'std::tuple_element_t': template parameter '_Index': 'I': a variable with non-static storage duration cannot be used as a non-type argument

我是否遗漏了这里的任何重要细节,或者这只是另一个 MSVC 错误?

Godbolt link

最佳答案

这是一个错误,Visual 在别名方面存在一些问题,这消除了 typename 的需要。 type_at

就是这种情况
template<std::size_t I>
using type_at = std::tuple_element_t<I, std::tuple<Ts...>>;

解决方法是将别名 type_at 用法替换为其别名:

using even_common_type = std::common_type_t<
type_at<(2u * Is)>
...>;

通过

using even_common_type = std::common_type_t<
typename std::tuple_element<2 * Is, std::tuple<Ts...>>::type
...>;

Demo

关于c++ - MSVC : a variable with non-static storage duration cannot be used as a non-type argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50750642/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com