2013-03-13 49 views
2

我似乎遇到了编译器/库错误。当我尝试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 
       << "\n" 
       << "is_copy_constructible: " 
       << std::is_copy_constructible<T>::value << "\n"; 
} 

与Visual Studio 2012 Update 1中,我得到

is_copy_assignable: 1 
is_copy_constructible: 1 

代替

is_copy_assignable: 0 
is_copy_constructible: 0 

是否有其他解决方案?

+0

还有一个相关的问题:https://stackoverflow.com/questions/34135409/why-does-is-copy-constructible-return-true-for-unique-ptr-in-msvc12 – stgatilov 2018-03-04 07:05:27

回答

3

这当然是错误 MS的执行std::unique_ptr<>或类型特征。这些类型特征应该如你所期望的那样工作(参见实例here)。

+0

我认为代码也失败与公众的复制ctor。 – 2013-03-13 21:53:06

+0

@JesseGood:你说得对,谢谢你指出。 – 2013-03-13 21:55:59

3

如果您往下看发生了什么,它会检查赋值运算符和ctor 是否存在,这就是全部。在MSVC的实现中,复制构造函数和赋值运算符是privatestd::unique_ptr的实现。但是它不会进行任何访问检查。 MSVC尚未实施= delete等,这就是为什么它返回true

与它玩了一会儿后,我觉得我有什么,应该用于检测私人拷贝构造函数工作:

#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

但是,这造成了ICE与MSVC ...所以不起作用。

相关问题