MSVC - 如何确定是否必须移动类型? [英] MSVC - How can I find out if a type must be moved?

查看:171
本文介绍了MSVC - 如何确定是否必须移动类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎遇到了编译器/库错误的问题。当我尝试

  #include< iostream> 
#include< type_traits>
#include< memory>

int main()
{
typedef std :: unique_ptr< int> T;

std :: cout<< is_copy_assignable:
<< std :: is_copy_assignable< T> :: value
<< \\\

<< is_copy_constructible:
<< std :: is_copy_constructible< T> :: value<< \\\
; Visual Studio 2012更新1中的
}

我获得

  is_copy_assignable:1 
is_copy_constructible:1

而不是

  is_copy_assignable:0 
is_copy_constructible:0
解决方案

>如果你看看发生了什么,它检查赋值运算符和ctor 是否存在,这就是所有。在MSVC的实现中,拷贝构造函数和赋值运算符是 std :: unique_ptr 的实现中的 private 。但是它不做任何访问检查。 MSVC没有实现 = delete 等,这是为什么它返回 true



玩了一段时间后,我想我应该能够检测到私人复制的ctor:

  #include  #include< memory> 

template< typename T>
struct wrap
{
T t;
};

template< typename T,typename = int>
struct is_copyable:std :: false_type {};

template< typename T>
struct is_copyable< T,
decltype(wrap< T>(std :: declval< const wrap< T>&>()),0) :std :: true_type
{};

class X
{
public:
X(const X&){}
};

class Y
{
Y(const Y&){}
};

int main()
{
static_assert(is_copyable< std :: unique_ptr< int>> :: value,Error!
static_assert(is_copyable< X> :: value,Error!);
static_assert(is_copyable< Y> value,Error!);
}

LWS上的示例



,导致 ICE with MSVC ...所以不工作。


I seem to be having problems with a compiler/library bug. When I try

#include <iostream>
#include <type_traits>
#include <memory>

int main() 
{
    typedef std::unique_ptr<int> T;

    std::cout << "is_copy_assignable: " 
              << std::is_copy_assignable<T>::value 
              << "\n"
              << "is_copy_constructible: " 
              << std::is_copy_constructible<T>::value << "\n";
}

with Visual Studio 2012 Update 1 I get

is_copy_assignable: 1
is_copy_constructible: 1

instead of

is_copy_assignable: 0
is_copy_constructible: 0

Is there an alternative solution?

解决方案

If you look down into what is happening, it checks for whether the assignment operator and ctor exists and that is all. In MSVC's implementation, the copy constructor and assignment operator are private in std::unique_ptr's implementation. However it does not do any access checking. MSVC has not implemented = delete, etc. which is why it returns true.

After playing with it for a while, I think I have something that should work for detecting private copy ctor:

#include <type_traits>
#include <memory>

template <typename T>
struct wrap
{
   T t;
};

template<typename T, typename = int>
struct is_copyable : std::false_type { };

template<typename T>
struct is_copyable<T, 
    decltype(wrap<T>(std::declval<const wrap<T>&>()), 0)> : std::true_type
{};

class X
{
   public:
   X(const X&) {}
};

class Y
{
   Y(const Y&){}
};

int main()
{
    static_assert(is_copyable<std::unique_ptr<int>>::value, "Error!");
    static_assert(is_copyable<X>::value, "Error!");
    static_assert(is_copyable<Y>::value, "Error!");
}

Example on LWS.

However, this caused an ICE with MSVC... so doesn't work.

这篇关于MSVC - 如何确定是否必须移动类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆