MSVC - How can I find out if a type must be moved? [duplicate]

烈酒焚心 提交于 2019-12-24 03:28:14

问题


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?


回答1:


This is most certainly a bug of MS's implementation of either std::unique_ptr<> or of the type traits. Those type traits should work as you expect (see a live example here).




回答2:


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.



来源:https://stackoverflow.com/questions/15396193/msvc-how-can-i-find-out-if-a-type-must-be-moved

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!