问题
I'm playing with the proposal of standard library support for the C++ detection idiom and compiled the following code with the Microsoft C/C++ Optimizing Compiler Version 19.00.23725 for x64:
#include <iostream>
template<class...>
using void_t = void;
template<class, template<class> class, class = void_t<>>
struct detect : std::false_type { };
template<class T, template<class> class Operation>
struct detect<T, Operation, void_t<Operation<T>>> : std::true_type { };
template<class T>
using bar_t = decltype(std::declval<T>().bar());
template<class T>
using bar_int_t = decltype(std::declval<T>().bar(0));
template<class T>
using bar_string_t = decltype(std::declval<T>().bar(""));
struct foo
{
int bar() { return 0; }
int bar(int) { return 0; }
};
int main()
{
std::cout << detect<foo, bar_t>{} << std::endl;
std::cout << detect<foo, bar_int_t>{} << std::endl;
std::cout << detect<foo, bar_string_t>{} << std::endl;
return 0;
}
The expected output would be
1
1
0
but it is
1
1
1
What is going wrong? I've made a live demo (with exactly the same code) where the output is as expected.
回答1:
Visual C++ has a limited support for expression SFINAE, however, I think it should work with a function return type. So, instead you can try the below implementation:
#include <type_traits>
template <typename...>
using void_t = void;
template <typename, template <typename> class>
auto detect_impl(char) -> std::false_type;
template <typename T, template <typename> class Operation>
auto detect_impl(int) -> decltype(void_t<Operation<T>>(), std::true_type{});
template <typename T, template <typename> class Operation>
using detect = decltype(detect_impl<T, Operation>(0));
(Tested on http://webcompiler.cloudapp.net/)
来源:https://stackoverflow.com/questions/35669239/the-c-detection-idiom-is-not-working-as-expected-in-visual-studio-2015-update