小编war*_*onk的帖子

std::reference_wrapper,构造函数实现解释

我一直试图从这里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)

c++ templates decltype reference-wrapper declval

8
推荐指数
1
解决办法
836
查看次数

使用“&lt;”运算符而不是“==”运算符的 std::set&lt;Key,Compare,Allocator&gt;::find() 函数背后的直觉是什么?

为什么在一组自定义类(假设 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)

c++ stl c++14

5
推荐指数
1
解决办法
97
查看次数

标签 统计

c++ ×2

c++14 ×1

decltype ×1

declval ×1

reference-wrapper ×1

stl ×1

templates ×1