Is there a reason declval returns add_rvalue_reference instead of add_lvalue_reference

前端 未结 4 1415
无人及你
无人及你 2020-12-13 18:18

changing a type into a reference to a type, allows one to access the members of the type without creating an instance of the type. This seems to be true for bot

4条回答
  •  天命终不由人
    2020-12-13 19:03

    An example of where you need control over the returned type can be found in my df.operators library, when you need to provide a noexcept specification. Here's a typical method:

    friend T operator+( const T& lhs, const U& rhs )
      noexcept( noexcept( T( lhs ),
                          std::declval< T& >() += rhs,
                          T( std::declval< T& >() ) ) )
    {
        T nrv( lhs );
        nrv += rhs;
        return nrv;
    }
    

    In generic code, you need to be exact about what you are doing. In the above, T and U are types outside of my control and the noexcept specification for a copy from a const lvalue reference, a non-const lvalue reference and an rvalue reference could be different. I therefore need to be able to express cases like:

    • Can I construct T from a T&? (Use T(std::declval()))
    • Can I construct T from a const T&? (Use T(std::declval()))
    • Can I construct T from a T&&? (Use T(std::declval()))

    Luckily, std::declval allows the above by using std::add_rvalue_reference and reference the collapsing rules.

提交回复
热议问题