Using declval with a reference type
Asked Answered
S

1

6

I see some code examples where the type used to instantiate the std::declval template function is specified as a reference type rather than just a type, as in:

std::declval<T &>()

as opposed to:

std::declval<T>()

where T is some type. I am missing the subtlety why the reference notation might be chosen overe the plain type. Would someone please explain this to me ?

I know that std::declval expands to typename std::add_rvalue_reference<T>::type but I am still missing the reason why one would instantiate the latter with a reference to a type rather than the plain type itself.

Stipule answered 28/4, 2021 at 20:18 Comment(3)
Does this answer your question? Is there a reason declval returns add_rvalue_reference instead of add_lvalue_referenceHomogenous
No. Enrico, that has nothing to do with my question.Stipule
what about my answer?Homogenous
H
4

Due to reference collapsing the result is different (see the answer I linked in a comment), and it does have consequences.

Think of the fact that memeber functions can be &&/&/const& qualified, for instance.

The following is a simple, even though probably meaningless, example of how passing T vs T& to std::declval can have a "drastic" effect.

#include <type_traits>
struct A{};
struct B{};

struct C {
    A f() && { return A{}; }
    B f() const & { return B{}; }
};
int main() {
    static_assert(std::is_same_v<A, decltype(std::declval<C>().f())>);
    static_assert(std::is_same_v<B, decltype(std::declval<C&>().f())>);
}
Homogenous answered 28/4, 2021 at 20:27 Comment(1)
This is understandable, Enlico. Thanks !Stipule

© 2022 - 2024 — McMap. All rights reserved.