C++ std::experimental::is_detected 在 MSVC 中不工作

C++ std::experimental::is_detected not working in MSVC

我有一些代码允许将值转换为字符串,这在 g++ 和 CLion 中完美运行,但是当我尝试 运行 与 MSVC 在 Visual Studio 中的相同程序时该程序给出了很多错误,其中一些是非常奇怪的语法错误。

这是我使用的代码:

// 1- detecting if std::to_string is valid on T
 
template<typename T>
using std_to_string_expression = decltype(std::to_string(std::declval<T>()));
 
template<typename T>
constexpr bool has_std_to_string = is_detected<std_to_string_expression, T>;
 
 
// 2- detecting if to_string is valid on T
template<typename T>
using to_string_expression = decltype(to_string(std::declval<T>()));
 
template<typename T>
constexpr bool has_to_string = is_detected<to_string_expression, T>;
 
 
// 3- detecting if T can be sent to an ostringstream
 
template<typename T>
using ostringstream_expression = decltype(std::declval<std::ostringstream&>() << std::declval<T>());
 
template<typename T>
constexpr bool has_ostringstream = is_detected<ostringstream_expression, T>;



// -----------------------------------------------------------------------------


// 1-  std::to_string is valid on T
template<typename T, typename std::enable_if<has_std_to_string<T>, int>::type = 0>
std::string toString(T const& t) {
    return std::to_string(t);
}
 
// 2-  std::to_string is not valid on T, but to_string is
template<typename T, typename std::enable_if<!has_std_to_string<T> && has_to_string<T>, int>::type = 0>
std::string toString(T const& t) {
    return to_string(t);
}
 
// 3-  neither std::string nor to_string work on T, let's stream it then
template<typename T, typename std::enable_if<!has_std_to_string<T> && !has_to_string<T> && has_ostringstream<T>, int>::type = 0>
std::string toString(T const& t) {
    std::ostringstream oss;
    oss << t;
    return oss.str();
}

我想知道我是否做错了什么非常明显的错误,或者是否有更复杂的事情导致了这个问题。我需要更改什么才能使此程序在 Visual Studio 中运行并正确编译?

std::experimental::is_detected is not supported in Visual Studio 2019 or earlier. You can pretty easily write your own cross platform implementation of it though, as demonstrated here: https://topanswers.xyz/cplusplus?q=1295