VC ++错误,编译时序列操作

时间:2019-02-01 18:44:48

标签: c++ templates visual-c++ c++17 variadic-templates

我正在尝试为编译时积分序列实现某些功能。下面是该操作的实现或:

  events: [
               {% for i in events %}
                     {
                        resourceId:'{{i.resource_id}}',
                        title: '{{ i.event_name}}',
                        start: '{{ i.start_date|date:"Y-m-d" }}',
                        end: '{{ i.end_date|date:"Y-m-d" }}',
                      },

               {% endfor %}

               ],

我尝试了几种情况,通过VC ++(19.16.27026.1)检查程序,很多情况下导致编译器错误:

#include <cstdint>
#include <utility>

using UInt32 = std::uint32_t;

template<UInt32... s>
using Sequence = std::integer_sequence<UInt32, s...>;

template<UInt32>
constexpr bool find([[maybe_unused]] Sequence<> a = {}) noexcept {
    return false;
}

template<UInt32 x, UInt32 v, UInt32... s>
constexpr bool find([[maybe_unused]] Sequence<v, s...> a = {}) noexcept {
    if constexpr (v == x) {
        return true;
    } else {
        return find<x, s...>();
    }
}

constexpr auto operator|(Sequence<>, Sequence<>) noexcept {
    return Sequence<>{};
}

template<UInt32 v, UInt32... s>
constexpr auto operator|(Sequence<v, s...>, Sequence<>) noexcept {
    return Sequence<v, s...>{};
}

template<UInt32... s, UInt32 x, UInt32... t>
constexpr auto operator|(Sequence<s...>, Sequence<x, t...>) noexcept {
    if constexpr (find<x, s...>()) {
        return Sequence<s...>{} | Sequence<t...>{};
    } else {
        return Sequence<s..., x>{} | Sequence<t...>{}; // error C2679
    }
}

例如:

Error C2679 binary '|': no operator found which takes a right-hand operand of type 'std::integer_sequence<uint32_t,4,5>' (or there is no acceptable conversion)

我尝试了其他编译​​器,例如GCC 8.2.0和Clang 7.0.0。他们会编译所有没有错误的案例。

我不明白为什么会发生此错误。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

我了解为什么会发生错误。

考虑以下示例:

#include <cstdint>
#include <utility>

using UInt32 = std::uint32_t;

template<UInt32... s>
using Sequence = std::integer_sequence<UInt32, s...>;

template<UInt32... s, UInt32 x, UInt32... t>
constexpr auto foo(Sequence<s...>, Sequence<x, t...>) noexcept {
    return 0;
}

int main() {
    [[maybe_unused]] constexpr auto a = foo(Sequence<1, 2>{}, Sequence<3, 4, 5>{});
    [[maybe_unused]] constexpr auto b = foo(Sequence<1, 2, 3>{}, Sequence<4, 5>{});
}

GCC 8.2.0和Clang 7.0.0编译成功编译了它。但VC ++(VS 2017)生成以下错误:

Error C2664: 'int func<1,2,3,4,5>(std::integer_sequence<uint32_t,1,2>,std::integer_sequence<uint32_t,3,4,5>) noexcept': cannot convert argument 1 from 'std::integer_sequence<uint32_t,1,2,3>' to 'std::integer_sequence<uint32_t,1,2>'

从错误中您可以看到,由于函数模板的参数列表和模板化类型的重合,编译器尝试ba使用已经定义的函数,导致失败的原因是,最终的功能参数类型不同。其他两个编译器定义了一个新函数,其中包含b的新类型:

a: foo<1, 2, 3, 4, 5>(Sequence<1, 2>, Sequence<3, 4, 5>)
b: foo<1, 2, 3, 4, 5>(Sequence<1, 2, 3>, Sequence<4, 5>)

因此,在计算Sequence<1>{} | Sequence<3, 4, 5>{}时出现以下情况:

constexpr auto operator|<1, 2, 3, 4, 5>(Sequence<1, 2>, Sequence<3, 4, 5>) noexcept {
    if constexpr (find<3, 1, 2>()) { // false
        return Sequence<1, 2>{} | Sequence<3, 4, 5>{};
    } else {
        return Sequence<1, 2, 3>{} | Sequence<4, 5>{}; // error C2679
    }
}

试图使用编译器operator|<1, 2, 4, 5>(Sequence<1, 2>, Sequence<3, 4, 5>)

现在知道了问题的实质,我能够改写的解决方案,以便它可以由所有三个编译器编译:

using UInt32 = std::uint32_t;

template<UInt32... s>
struct Sequence {};

template<template<UInt32...> typename S, UInt32... s>
constexpr bool empty([[maybe_unused]] S<s...> a = {}) noexcept {
    return sizeof...(s) == 0;
}

template<template<UInt32...> typename S, UInt32 x, UInt32... s>
constexpr UInt32 first([[maybe_unused]] S<x, s...> a = {}) noexcept {
    return x;
}

template<template<UInt32...> typename S>
constexpr auto tail([[maybe_unused]] S<> a = {}) noexcept {
    return S<>{};
}

template<template<UInt32...> typename S, UInt32 x, UInt32... s>
constexpr auto tail([[maybe_unused]] S<x, s...> a = {}) noexcept {
    return S<s...>{};
}

template<UInt32 x, template<UInt32...> typename S>
constexpr bool find([[maybe_unused]] S<> a = {}) noexcept {
    return false;
}

template<UInt32 x, template<UInt32...> typename S, UInt32 v, UInt32... s>
constexpr bool find([[maybe_unused]] S<v, s...> a = {}) noexcept {
    if constexpr (v == x) {
        return true;
    } else {
        return find<x>(S<s...>{});
    }
}

template<UInt32... s, typename S2>
constexpr auto operator|(Sequence<s...>, S2) noexcept {
    if constexpr (sizeof...(s) == 0) {
        return S2{};
    } else if constexpr (empty(S2{})) {
        return Sequence<s...>{};
    } else {
        constexpr auto x = first(S2{});
        if constexpr (find<x>(Sequence<s...>{})) {
            return Sequence<s...>{} | tail(S2{});
        } else {
            return Sequence<s..., x>{} | tail(S2{});
        }
    }
}