c++ - MSVC 无法评估 enable_if 中的 constexpr 函数

标签 c++ visual-c++ c++14

考虑一个简单的效用函数来计算合取,并使用这个效用来确保 std::tuple 中的类型都相等。

#include <type_traits>
#include <tuple>

constexpr auto all() noexcept -> bool { return true; }

template <class... Bools>
constexpr auto all(bool const x, Bools... xs) noexcept -> bool
{
    return x && all(xs...);
}

template <class T, class = void>
struct foo;

template <class T, class... Ts>
struct foo< std::tuple<T, Ts...>
          , std::enable_if_t<all(std::is_same<T, Ts>::value...)>
          > {
};

int main()
{
    foo<std::tuple<int, int>> x;
}

GCC 和 Clang 可以处理这段代码,但 MSVC 不行。 Here's神 bolt 链接。所以我想知道,这是一个 MSVC 错误还是只是我遗漏了什么?

最佳答案

我猜这是一个 MSVC 错误。如果支持 C++17,我建议改为使用 std::conjunction .在 C++14 下,作为一种解决方法,可以从前一个链接复制“可能的实现”:

#include <type_traits>
#include <tuple>

template<class...> struct conjunction : std::true_type { };
template<class B1> struct conjunction<B1> : B1 { };
template<class B1, class... Bn>
struct conjunction<B1, Bn...> 
    : std::conditional_t<bool(B1::value), conjunction<Bn...>, B1> {};
template<class... B>
constexpr bool conjunction_v = conjunction<B...>::value;

template <class T, class = void>
struct foo;

template <class T, class... Ts>
struct foo< typename std::tuple<T, Ts...>
        , std::enable_if_t<conjunction_v<std::is_same<T, Ts>...>>
        > {
};

int main()
{
    foo<std::tuple<int, int>> x;
}

关于c++ - MSVC 无法评估 enable_if 中的 constexpr 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51226444/

相关文章:

visual-c++ - MSVC 中的链接错误 LNK2019,带有 __imp__ 前缀的未解析符号,但应该来自静态库

c++ - 在std::bind中引用是没有用的?

c++ - 如果我们将 "decltype(auto) f()"用作定义中包含 "decltype(f()) result"的函数声明,会发生什么情况?

matlab - 如何使用 CMake 和 Visual Studio 2010(64 位)构建 MATLAB R2011a(64 位)mex 文件?

c++ - 为什么这个 vector 声明无效?

c++ - 在字典中查找单词模式,高性能

c++ - 在 QPlainTextEdit 中通过 Ctrl+Wheel 启用文本缩放

c++ - 为什么 al_draw_textf() 打印数字而不是存储在字符串变量中的字母?

c++ - 在多个模板上重复模板enable_if条件

c++ - CodeBlocks为32位计算机编译程序