Tobias Hermann
Tobias Hermann

Reputation: 10926

std::is_assignable and std::pair<const T, U>

As expected, the following code does not compile.

#include <type_traits>
#include <utility>
int main()
{
    using T = std::pair<const int, int>;
    const auto ok = std::is_assignable<T, T>::value; // true
    T x;
    T y;
    x = y; // compiler error
}

But the value of ok is true with the following three compilers.

Why is this?

Upvotes: 3

Views: 257

Answers (1)

T.C.
T.C.

Reputation: 137310

  1. is_assignable asks the question "is there an assignment operator signature that accepts these arguments", not "will that assignment operator actually compile" (in standardese, it only considers the immediate context of the assignment expression):

    template<class T>
    struct foo {
        T t {};
        foo& operator=(const foo& r) { t = r.t; };
    };
    static_assert(std::is_copy_assignable<foo<const int>>::value, ""); // OK
    
    void bar() {
        foo<const int> f1, f2;
        f1 = f2; // explodes
    }
    
  2. pair's assignment operators can't be defaulted, because it needs to do something special when the pair contains a reference. That means that additional precautions need to be taken to ensure that is_assignable doesn't lie (e.g., making sure that the copy assignment operator is deleted if a member type is not copy assignable). The standard has not mandated such precautions until very recently.

  3. is_assignable<T, T> asks whether a T rvalue can be assigned to a T rvalue. This is an odd question to ask.

Upvotes: 5

Related Questions