Tuple prevention outside of if constexpr

The following code compiles in GCC and Clang, but stops working with the latest Visual Studio update (/ std: C ++ latest):

#include <tuple>

template<int pos, typename... T>
void check_tuple(T... types) {
    if constexpr (pos <= -1) {
        // nothing
    } else {
        using Type = typename std::tuple_element<pos, std::tuple<T...>>::type;
    }
}

int main() {
    check_tuple<0>(1.0, 1.0);
    check_tuple<-1>(1.0, 1.0);
}

In the latest version of Visual Studio (/ std: C ++ latest), compilation failed with the tuple index out of bounds (std :: tuple_element <18446744073709551613, std :: tuple <→).

Is it possible to exclude a tuple outside with constexpr?

+4
source share
1 answer

This is a VS error (please tell Microsoft). The code should work as it is.


Until then, you can resort to how we used this problem: tag disptaching.

template<int pos, typename... T>
void check_tuple_impl(std::true_type, T... types) {
    // nothing
}

template<int pos, typename... T>
void check_tuple_impl(std::false_type, T... types) {
    using Type = typename std::tuple_element<pos, std::tuple<T...>>::type;
}

template<int pos, typename... T>
void check_tuple(T... types) {
    check_tuple_impl<pos>(std::integral_constant<bool, (pos <= -1)>{}, types...);
}
+5
source

Source: https://habr.com/ru/post/1690872/


All Articles