Skip to content

Commit

Permalink
Update README.md (AnthonyCalandra#136)
Browse files Browse the repository at this point in the history
* Update README.md

* Update CPP17.md

* Update CPP11.md

* Improve the ref-qualified member functions code even more.

---------

Co-authored-by: Anthony Calandra <anthony@anthony-calandra.com>
  • Loading branch information
roaming-debug and AnthonyCalandra authored Oct 11, 2024
1 parent 090b412 commit 9852478
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 20 deletions.
18 changes: 9 additions & 9 deletions CPP11.md
Original file line number Diff line number Diff line change
Expand Up @@ -619,23 +619,23 @@ struct Bar {
};

struct Foo {
Bar getBar() & { return bar; }
Bar getBar() const& { return bar; }
Bar getBar() && { return std::move(bar); }
Bar& getBar() & { return bar; }
const Bar& getBar() const& { return bar; }
Bar&& getBar() && { return std::move(bar); }
const Bar&& getBar() const&& { return std::move(bar); }
private:
Bar bar;
};

Foo foo{};
Bar bar = foo.getBar(); // calls `Bar getBar() &`
Bar bar = foo.getBar(); // calls `Bar& getBar() &`

const Foo foo2{};
Bar bar2 = foo2.getBar(); // calls `Bar Foo::getBar() const&`
Bar bar2 = foo2.getBar(); // calls `Bar& Foo::getBar() const&`

Foo{}.getBar(); // calls `Bar Foo::getBar() &&`
std::move(foo).getBar(); // calls `Bar Foo::getBar() &&`

std::move(foo2).getBar(); // calls `Bar Foo::getBar() const&&`
Foo{}.getBar(); // calls `Bar&& Foo::getBar() &&`
std::move(foo).getBar(); // calls `Bar&& Foo::getBar() &&`
std::move(foo2).getBar(); // calls `const Bar&& Foo::getBar() const&`
```
### Trailing return types
Expand Down
2 changes: 1 addition & 1 deletion CPP17.md
Original file line number Diff line number Diff line change
Expand Up @@ -369,7 +369,7 @@ std::vector v{ 1, 2, 3 }; // deduces std::vector<int>
std::mutex mtx;
auto lck = std::lock_guard{ mtx }; // deduces to std::lock_guard<std::mutex>

auto p = new std::pair{ 1.0, 2.0 }; // deduces to std::pair<double, double>
auto p = new std::pair{ 1.0, 2.0 }; // deduces to std::pair<double, double>*
```
For user-defined types, *deduction guides* can be used to guide the compiler how to deduce template arguments if applicable:
Expand Down
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,7 @@ std::vector v{ 1, 2, 3 }; // deduces std::vector<int>
std::mutex mtx;
auto lck = std::lock_guard{ mtx }; // deduces to std::lock_guard<std::mutex>

auto p = new std::pair{ 1.0, 2.0 }; // deduces to std::pair<double, double>
auto p = new std::pair{ 1.0, 2.0 }; // deduces to std::pair<double, double>*
```
For user-defined types, *deduction guides* can be used to guide the compiler how to deduce template arguments if applicable:
Expand Down Expand Up @@ -2096,23 +2096,23 @@ struct Bar {
};

struct Foo {
Bar getBar() & { return bar; }
Bar getBar() const& { return bar; }
Bar getBar() && { return std::move(bar); }
Bar& getBar() & { return bar; }
const Bar& getBar() const& { return bar; }
Bar&& getBar() && { return std::move(bar); }
const Bar&& getBar() const&& { return std::move(bar); }
private:
Bar bar;
};

Foo foo{};
Bar bar = foo.getBar(); // calls `Bar getBar() &`
Bar bar = foo.getBar(); // calls `Bar& getBar() &`

const Foo foo2{};
Bar bar2 = foo2.getBar(); // calls `Bar Foo::getBar() const&`
Bar bar2 = foo2.getBar(); // calls `Bar& Foo::getBar() const&`

Foo{}.getBar(); // calls `Bar Foo::getBar() &&`
std::move(foo).getBar(); // calls `Bar Foo::getBar() &&`

std::move(foo2).getBar(); // calls `Bar Foo::getBar() const&&`
Foo{}.getBar(); // calls `Bar&& Foo::getBar() &&`
std::move(foo).getBar(); // calls `Bar&& Foo::getBar() &&`
std::move(foo2).getBar(); // calls `const Bar&& Foo::getBar() const&`
```
### Trailing return types
Expand Down

0 comments on commit 9852478

Please sign in to comment.