std::any::emplace(3)
π std::any::emplace(3) β C++ Standard Library
okay bestie, gather round, because this one is so useful and also lowkey the most "i contain multitudes" function in the whole standard library. it's std::any::emplace, from the C++ Standard Library, and it's giving main character energy β¨
πΈ NAME πΈ
std::any::emplace β std::any::emplace π
(yes, the name is the name, we love a self-titled icon)
β¨ Synopsis β¨
template< class ValueType, class... Args >
(1) (since C++17)
std::decay_t<ValueType>& emplace( Args&&... args );
template< class ValueType, class U, class... Args >
std::decay_t<ValueType>& emplace( std::initializer_list<U> il,
(2) (since C++17)
Args&&... args );
So what's the vibe? It changes the contained object to one of type std::decay_t<ValueType>, constructed from the arguments. That's it, that's the whole personality π«Ά
First it destroys the current contained object (if any) by reset(), then:
- constructs an object of type
std::decay_t<ValueType>, direct-non-list-initialized fromstd::forward<Args>(args)..., as the contained object.- This overload participates in overload resolution only if
std::is_constructible_v<std::decay_t<ValueType>, Args...>andstd::is_copy_constructible_v<std::decay_t<ValueType>>are bothtrue.
- This overload participates in overload resolution only if
- constructs an object of type
std::decay_t<ValueType>, direct-non-list-initialized fromil,std::forward<Args>(args)..., as the contained object.- This overload participates in overload resolution only if
std::is_constructible_v<std::decay_t<ValueType>, std::initializer_list<U>&, Args...>andstd::is_copy_constructible_v<std::decay_t<ValueType>>are bothtrue.
- This overload participates in overload resolution only if
β οΈ important note from the group chat: it direct-non-list-initializes, so no sneaky brace-init business happening behind the scenes. and remember the old object gets nuked before the new one is built β we'll get to why that matters in a sec π
π Template parameters π
ValueTypeβ contained value type (that's the type you're stuffing into theany, bestie)
π¦ Type requirements π¦
std::decay_t<ValueType>must meet the requirements ofCopyConstructible.
no cap, that's a hard requirement. it's non-negotiable, it's the dress code, it's the vibe check π
π Return value π
A reference to the new contained object. you get a hand-delivered reference to your shiny new thing, go off king π
π Exceptions π
Throws any exception thrown by T's constructor. and if an exception is thrown?
okay so this is the part where you gotta pay attention π β the previously contained object (if any) has already been destroyed, and *this does not contain a value.
π¨ so like, there's no undo button. the old object is gone gone. if the constructor throws, you're left with an empty any. no strong exception guarantee for you, bestie.
π Example π
// Run this code
#include <algorithm>
#include <any>
#include <iostream>
#include <string>
#include <vector>
class Star
{
std::string name;
int id;
public:
Star(std::string name, int id) : name{name}, id{id}
{
std::cout << "Star::Star(string, int)\n";
}
void print() const
{
std::cout << "Star{\"" << name << "\" : " << id << "};\n";
}
};
int main()
{
std::any celestial;
// (1) emplace(Args&&... args);
celestial.emplace<Star>("Procyon", 2943);
const auto* star = std::any_cast<Star>(&celestial);
star->print();
std::any av;
// (2) emplace(std::initializer_list<U> il, Args&&... args);
av.emplace<std::vector<char>>({'C', '+', '+', '1', '7'} /* no args
*/);
std::cout << av.type().name() << '\n';
const auto* va = std::any_cast<std::vector<char>>(&av);
std::for_each(va->cbegin(), va->cend(), [](char const& c) {
std::cout << c; });
std::cout << '\n';
}
look at her go πβοΈ the first one's the plain args overload, the second one's the initializer_list overload with literally zero extra args, and she still slays.
π Possible output π
Star::Star(string, int)
Star{"Procyon" : 2943};
St6vectorIcSaIcEE
C++17
(the St6vectorIcSaIcEE is just the mangled type name being its weird little self, don't worry about it π)
π See also π
- constructor β constructs an
anyobject (public member function) - reset β destroys contained object (public member function)
that's the whole page, bestie, you're officially an emplace expert now, go build something unhinged π
source: cppreference.com Β· version 2024.06.10
xoxo, and remember β emplace responsibly, because the old value is never coming back ππ
β¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms