declval<T> 与 declval<T&>

declval<T> vs declval<T&>

我想了解 declval<T>()declval<T&>() 之间的区别?有没有可以使用 T&T 不能的例子?

#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>);
}

Is there an example where T& can be used while T cannot?

是的,例如在这样的情况下,您有 ref-qualified 重载:

struct X {
    X() = delete;
    int func() &;
    double func() &&;
};

int main() {
    // this static_assert would fail:
    static_assert(std::is_same_v<decltype(std::declval<X>().func()), int>);
}

除了 ref 限定的成员函数(很少使用),std::declval<T&>() 更常见的用例是创建左值引用(否则会创建右值引用)。

#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
}