2

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?

ildjarn
  • 62,044
  • 9
  • 127
  • 211
Thomas
  • 4,980
  • 2
  • 15
  • 30
  • One more related issue: https://stackoverflow.com/questions/34135409/why-does-is-copy-constructible-return-true-for-unique-ptr-in-msvc12 – stgatilov Mar 04 '18 at 07:05

2 Answers2

3

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).

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
3

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.

Jesse Good
  • 50,901
  • 14
  • 124
  • 166