小编Fed*_*uro的帖子

void_t在Visual Studio 2015上失败

我不明白为什么以下测试总是因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)

我知道 …

c++ sfinae c++14 visual-studio-2015

18
推荐指数
1
解决办法
1206
查看次数

标签 统计

c++ ×1

c++14 ×1

sfinae ×1

visual-studio-2015 ×1