std::move_if_noexcept
提供:cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
| Defined in header <utility>
|
||
| template< class T > typename std::conditional< |
(C + + 11以来) | |
そのムーブコンストラクタは例外をスローしない場合
move_if_noexceptがその引数に右辺値参照を取得し、そうでなければ、その引数に左辺値参照を取得します。これは一般的に強い例外保証付き移動セマンティクスを結合するために使用され.Original:
move_if_noexcept obtains an rvalue reference to its argument if its move constructor does not throw exceptions, otherwise obtains an lvalue reference to its argument. It is typically used to combine move semantics with strong exception guarantee.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
たとえば、std::vector::resizeは時折新しいストレージし、古いストレージから新しいストレージへ移動またはコピーする要素を割り当てます。例外はこの操作中に発生した場合は、std::vector::resizestd::move_if_noexceptが移動またはコピー建設工事のどちらを使用するかを決定するために使用された場合にのみ可能ですこの時点までそれがなかったすべてのものを、元に戻し.
Original:
For example, std::vector::resize occasionally allocates new storage and then moves or copies elements from old storage to new storage. If an exception occurs during this operation, std::vector::resize undoes everything it did to this point, which is only possible if std::move_if_noexcept was used to decide whether to use move construction or copy construction.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
目次 |
[編集] パラメータ
| x | - | オブジェクトが移動またはコピーされます
Original: the object to be moved or copied The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. |
[編集] 値を返します
std::move(x)またはx、例外の保証に応じて.
Original:
std::move(x) or x, depending on exception guarantees.
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[編集] 例外
[編集] 例
#include <iostream> #include <utility> struct Bad { Bad() {} Bad(Bad&&) // may throw { std::cout << "Throwing move constructor called\n"; } Bad(const Bad&) // may throw as well { std::cout << "Throwing copy constructor called\n"; } }; struct Good { Good() {} Good(Good&&) noexcept // will NOT throw { std::cout << "Non-throwing move constructor called\n"; } Good(const Good&) {}; }; int main() { Good g; Bad b; Good g2 = std::move_if_noexcept(g); Bad b2 = std::move_if_noexcept(b); }
Output:
Non-throwing move constructor called Throwing copy constructor called
[編集] 複雑
定数
Original:
Constant
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[編集] も参照してください
| (C++11) |
転送関数の引数 Original: forwards a function argument The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (関数テンプレート) |
| (C++11) |
右辺値参照を取得します Original: obtains an rvalue reference The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. (関数テンプレート) |