我不明白为什么以下测试总是因Visual Studio 2015(static_assert触发器)而失败:
#include <type_traits>
using namespace std;
template<class T> using try_assign = decltype(declval<T&>() = declval<T const&>());
template<class, class = void> struct my_is_copy_assignable : false_type {};
template<class T> struct my_is_copy_assignable<T, void_t<try_assign<T>>> : true_type {};
int main()
{
static_assert(my_is_copy_assignable<int>::value, "fail");
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它基本上是Walter E Brown在他的cppcon 2014演示文稿"现代模板元编程 - 概要"中对void_t的使用示例的转录.
重要的是要注意这个替代版本的作用,所以我不认为问题在于MSVC对表达SFINAE的不完全支持.
template<class T>
using try_assign = decltype(declval<T&>() = declval<T const&>());
template<class T>
struct my_is_copy_assignable
{
template<class Q, class = try_assign<Q>>
static true_type tester(Q&&);
static false_type tester(...);
using type = decltype(tester(declval<T>()));
};
Run Code Online (Sandbox Code Playgroud)
我知道 …