GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • cpprefjp - C++日本語リファレンス
  • GitHub Project

This view

  • Original page
  • New address

GeezerVison is showing cpprefjp.github.io — std::stack::emplace - cpprefjp C++日本語リファレンス

  1. リファレンス
  2. stack
  3. stack
  4. emplace

最終更新日時: 2026年09月11日 13時06分31秒 (JST)
Akira Takahashi が更新

履歴 編集

function template
<stack>

std::stack::emplace(C++11)

template <class... Args>
void emplace(Args&&... args);                     // (1) C++11
template <class... Args>
decltype(auto) emplace(Args&&... args);           // (1) C++17
template <class... Args>
constexpr decltype(auto) emplace(Args&&... args); // (1) C++26

概要

要素型 value_type のコンストラクタ引数をとり、直接構築でスタックに要素を追加する。

効果

  • C++14 まで

    c.emplace_back(std::forward<Args>(args)...);
    

  • C++17 から

    return c.emplace_back(std::forward<Args>(args)...);
    

戻り値

  • C++14 まで:なし
  • C++17 から:構築した要素への参照

例

#include <iostream>
#include <stack>
#include <string>
#include <utility>

int main()
{
  std::stack<std::pair<int, std::string>> st;

  st.emplace(3, "aaa");
  st.emplace(1, "bbb");
  st.emplace(4, "ccc");

  while (!st.empty()) {
    std::cout << st.top().first << ", " << st.top().second << std::endl;
    st.pop();
  }
}

出力

4, ccc
1, bbb
3, aaa

バージョン

言語

  • C++11

処理系

  • Clang: 3.2 ✅
  • GCC: 4.4 ✅
  • Visual C++: ??

備考

C++17 規格が発行された段階では戻り値型が reference に変更されていたが、そうしてしまうと emplace_back が参照を返さないコンテナと共に使用している既存のコードを破壊してしまうため、規格の誤りとして戻り値型が更に decltype(auto) に変更された。

関連項目

名前 説明
push 要素を追加する

参照

  • N2680 Proposed Wording for Placement Insert (Revision 1)
  • P0084R0 Emplace Return Type
  • P0084R1 Emplace Return Type (Revision 1)
  • P0084R2 Emplace Return Type (Revision 2)
  • LWG Issue 2783. stack::emplace() and queue::emplace() should return decltype(auto)
  • P3372R3 constexpr containers and adaptors
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.