I'm trying to understand the difference between declval<T>()
and declval<T&>()
? Is there an example where T&
can be used while T
cannot?
#include <type_traits>
#include <utility>
struct X {
X() = delete;
int func();
};
int main()
{
// works with both X as well as X& within declval
static_assert(std::is_same_v<decltype(std::declval<X&>().func()), int>);
}
Apart from ref-qualified member functions (which are used rarely), a more common use-case for std::declval<T&>()
is to create an lvalue reference (it creates an rvalue-reference otherwise).
#include <type_traits>
#include <utility>
struct X {};
int func(X&);
int main() {
static_assert(std::is_same_v<decltype(func(std::declval<X&>())), int>); // works
static_assert(std::is_same_v<decltype(func(std::declval<X>())), int>); // error
}