Fed*_*uro 18 c++ sfinae c++14 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)
我知道std::is_copy_assignable但我只是想更好地理解C++不同版本中可用的各种元编程技术.我在网上读了几篇关于void_t的帖子,但我仍然不明白为什么这个例子失败了.
有趣的是,使用GCC 4.8.2它工作正常(使用CWG 1558解决方法,这与微软的版本相同).
它是一个已知的Visual Studio错误,还是我做错了什么?
| 归档时间: |
|
| 查看次数: |
1206 次 |
| 最近记录: |