std::stack<T,Container>::emplace
From cppreference.com
template< class... Args > void emplace( Args&&... args ); |
(C++11以降) (C++17まで) |
|
| template< class... Args > decltype(auto) emplace( Args&&... args ); |
(C++17以降) | |
スタックの最上位に新しい要素をプッシュします。要素はインプレースで構築されます。つまり、コピーまたはムーブ操作は実行されません。要素のコンストラクタは、関数に渡された引数とまったく同じ引数で呼び出されます。
c.emplace_back(std::forward<Args>(args)...);を実質的に呼び出します。
目次 |
[編集] パラメータ
| args | - | 要素のコンストラクタに転送する引数 |
[編集] 戻り値
|
(なし) |
(C++17まで) |
|
上記の Container::emplace_back への呼び出しによって返される値、または参照(もしあれば)。 |
(C++17以降) |
[編集] 計算量
Container::emplace_back の計算量と同じです。
[編集] 例
このコードを実行
#include <iostream> #include <stack> struct S { int id; S(int i, double d, std::string s) : id{i} { std::cout << "S::S(" << i << ", " << d << ", \"" << s << "\");\n"; } }; int main() { std::stack<S> stack; const S& s = stack.emplace(42, 3.14, "C++"); // for return value C++17 required std::cout << "id = " << s.id << '\n'; }
出力
S::S(42, 3.14, "C++") id = 42
欠陥レポート
以下の動作変更を伴う欠陥報告が、以前に公開されたC++標準に遡って適用されました。
| DR | 適用対象 | 公開された動作 | 正しい動作 |
|---|---|---|---|
| LWG 2783 | C++17 | emplace は reference を返していたため、C++17 より前のコンテナとの互換性が損なわれていました。 |
decltype(auto) を返します。 |
[編集] 関連項目
| 要素をトップに挿入する (public member function) | |
| トップ要素を削除する (public member function) |