Alexander Bily
Alexander Bily

Reputation: 975

C++ Should this type satisfy std::is_copy_assignable?

With following code:

namespace _Detail
{
    struct _HasCopyAssignment
    {
        template <typename _Type>
        static auto test(_Type&& var) -> decltype(var = var, std::true_type());
        static auto test(...) -> decltype(std::false_type());
    };
}

template <typename _Type>
struct HasCopyAssignment : public decltype(_Detail::_HasCopyAssignment::test(std::declval<_Type>()))
{};

And this test type:

struct B
{
    B& operator=(B&);
};

my HasCopyAssignment<B>::value evaluates to true, but <type_traits> test for is_copy_assignable<B>::value in GCC standard lib implementation is false. Should object be considered copy assignable if it's copy assignment operator take a non-const reference as parameter?

Upvotes: 2

Views: 191

Answers (1)

libstdc++ is correct as marking this as not std::is_copy_assignable. Per C++14 20.10.4.3 table 49, is_copy_assignable is defined as:

For a referenceable type T, the same result as is_assignable<T, const T&>::value, otherwise false.

This means that the type trait std::is_copy_assignable<T> means "T can be assigned into with a const T& argument." Which your class cannot.

Upvotes: 4

Related Questions