Search code examples
c++assignment-operatorbuilt-in-types

Why std::declval<int>() = std::declval<int>() is invalid?


I'm trying to use detector I wrote which according to std::experiment::is_detected_v to check whether a type is assignable. However std::declval<int>() = std::declval<int>() is invalid and only std::declval<int&>() = std::declval<int>() is valid.

Why does this happen?


Solution

  • Look at the signature of std::declval:

    template<class T>
    typename std::add_rvalue_reference<T>::type declval() noexcept;
    

    std::declval<int>() has type int&&, an rvalue(xvalue). You can't assign to an rvalue int.