【问题标题】:MSVC: a variable with non-static storage duration cannot be used as a non-type argumentMSVC:具有非静态存储持续时间的变量不能用作非类型参数
【发布时间】:2018-06-07 21:51:23
【问题描述】:

考虑以下受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.1 和 clang 6.0,但在最新的 MSVC 版本中失败,并出现以下错误:

错误 C2971:“std::tuple_element_t”:模板参数“_Index”:“I”:具有非静态存储持续时间的变量不能用作非类型参数

我错过了这里的任何重要细节还是只是另一个 MSVC 错误?

Godbolt link

【问题讨论】:

    标签: c++ templates


    【解决方案1】:

    这是一个错误,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

    【讨论】:

      猜你喜欢
      • 2019-10-07
      • 1970-01-01
      • 2020-02-17
      • 2012-06-13
      • 2011-12-08
      • 2016-07-05
      • 2023-02-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多