mirror of
https://github.com/AnthonyCalandra/modern-cpp-features.git
synced 2025-12-17 01:54:36 +03:00
Explicit conversion functions.
This commit is contained in:
21
README.md
21
README.md
@@ -62,6 +62,7 @@ C++11 includes the following new language features:
|
||||
- [range-based for loops](#range-based-for-loops)
|
||||
- [special member functions for move semantics](#special-member-functions-for-move-semantics)
|
||||
- [converting constructors](#converting-constructors)
|
||||
- [explicit conversion functions](#explicit-conversion-functions)
|
||||
|
||||
C++11 includes the following new library features:
|
||||
- [std::move](#stdmove)
|
||||
@@ -975,6 +976,26 @@ A c = {0, 0}; // calls A::A(std::initializer_list<int>)
|
||||
A d{0, 0, 0}; // calls A::A(std::initializer_list<int>)
|
||||
```
|
||||
|
||||
### Explicit conversion functions
|
||||
Conversion functions can now be made explicit using the `explicit` specifier.
|
||||
```c++
|
||||
struct A {
|
||||
operator bool() const { return true; }
|
||||
};
|
||||
|
||||
struct B {
|
||||
explicit operator bool() const { return true; }
|
||||
};
|
||||
|
||||
A a{};
|
||||
if (a); // OK calls A::operator bool()
|
||||
bool ba = a; // OK copy-initialization selects A::operator bool()
|
||||
|
||||
B b{};
|
||||
if (b); // OK calls B::operator bool()
|
||||
bool bb = b; // error copy-initialization does not consider B::operator bool()
|
||||
```
|
||||
|
||||
## C++11 Library Features
|
||||
|
||||
### std::move
|
||||
|
||||
Reference in New Issue
Block a user