GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::any::emplace(3) C++ Standard Libary std::any::emplace(3)

std::any::emplace - std::any::emplace


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 );


Changes the contained object to one of type std::decay_t<ValueType> constructed from
the arguments.


First destroys the current contained object (if any) by reset(), then:


1) constructs an object of type std::decay_t<ValueType>, direct-non-list-initialized
from std::forward<Args>(args)..., as the contained object.


* This overload participates in overload resolution only if
std::is_constructible_v<std::decay_t<ValueType>, Args...> and
std::is_copy_constructible_v<std::decay_t<ValueType>> are both true.


2) constructs an object of type std::decay_t<ValueType>, direct-non-list-initialized
from il, 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...> and std::is_copy_constructible_v<std::decay_t<ValueType>> are both
true.


ValueType - contained value type


-
std::decay_t<ValueType> must meet the requirements of CopyConstructible.


A reference to the new contained object.


Throws any exception thrown by T's constructor. If an exception is thrown, the
previously contained object (if any) has been destroyed, and *this does not contain
a value.

// 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";
}
};


auto main() -> int
{
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';
}


Star::Star(string, int)
Star{ "Procyon" : 2943 };
St6vectorIcSaIcEE
C++17


constructor constructs an any object
(public member function)
reset destroys contained object
(public member function)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.