我一直试图从这里std::reference_wrapper理解 的实现,如下:
namespace detail {
template <class T> constexpr T& FUN(T& t) noexcept { return t; }
template <class T> void FUN(T&&) = delete;
}
template <class T>
class reference_wrapper {
public:
// types
typedef T type;
// construct/copy/destroy
template <class U, class = decltype(
detail::FUN<T>(std::declval<U>()),
std::enable_if_t<!std::is_same_v<reference_wrapper, std::remove_cvref_t<U>>>()
)>
constexpr reference_wrapper(U&& u) noexcept(noexcept(detail::FUN<T>(std::forward<U>(u))))
: _ptr(std::addressof(detail::FUN<T>(std::forward<U>(u)))) {}
reference_wrapper(const reference_wrapper&) noexcept = default;
// assignment
reference_wrapper& operator=(const reference_wrapper& x) noexcept = default;
// access
constexpr operator T& () const …Run Code Online (Sandbox Code Playgroud) 为什么在一组自定义类(假设 Person)上的 find() 函数调用不等式运算符 ' <' 而不是 ' ==' 。为了说明这一点,我有以下代码,我find在一组类 Person(请参阅test2())上调用该函数。.
#include<iostream>
#include<stdio.h>
#include<string>
#include<set>
using namespace std ;
class Person {
friend ostream & operator<<(ostream &os , const Person p) ;
string name ;
int age;
public :
Person()
:name{"Unknown"}, age{0}{
}
Person(string name , int age )
:name{name}, age{age}
{
}
//OVERLOADED operators
bool operator<(const Person &rhs) const;
bool operator ==(const Person &rhs) const;
};
bool Person::operator<(const Person &rhs) const{
cout<<" < …Run Code Online (Sandbox Code Playgroud)