Date: Tue, 15 Jun 2021 07:37:45 +0100
Hi,
For me the name tells "emplace if empty, do nothing otherwise". I think this would match try_emplace in other containers better.
But I don't think we need a member function for either behavior.
Cheers,
Lénárd
-------- Original Message --------
From: "Ryan P. Nicholl via Std-Proposals" <std-proposals_at_[hidden]>
Sent: June 14, 2021 11:26:25 PM GMT+01:00
To: std-proposals <std-proposals_at_[hidden]>
Cc: "Ryan P. Nicholl" <rnicholl_at_[hidden]>
Subject: [std-proposals] std::optional<T>::try_emplace
I would like to suggest "try_emplace" for std::optional,
which works like the following:
template <typename T, typename ... Ts>
void std::optional<T>::try_emplace(Ts && .. ts)
{
try
{
emplace(std::forward<Ts>(ts)...);
}
catch (...)
{}
}
Main use case is optional components which we want to eat exceptions if they fail to initialize:
std::optional<logger> m_logger;
...
m_logger.try_emplace(logs_directory);
...
if (m_logger.has_value())
{
m_logger->log(message);
}
else
{
std::cerr << message << std::endl;
}
For me the name tells "emplace if empty, do nothing otherwise". I think this would match try_emplace in other containers better.
But I don't think we need a member function for either behavior.
Cheers,
Lénárd
-------- Original Message --------
From: "Ryan P. Nicholl via Std-Proposals" <std-proposals_at_[hidden]>
Sent: June 14, 2021 11:26:25 PM GMT+01:00
To: std-proposals <std-proposals_at_[hidden]>
Cc: "Ryan P. Nicholl" <rnicholl_at_[hidden]>
Subject: [std-proposals] std::optional<T>::try_emplace
I would like to suggest "try_emplace" for std::optional,
which works like the following:
template <typename T, typename ... Ts>
void std::optional<T>::try_emplace(Ts && .. ts)
{
try
{
emplace(std::forward<Ts>(ts)...);
}
catch (...)
{}
}
Main use case is optional components which we want to eat exceptions if they fail to initialize:
std::optional<logger> m_logger;
...
m_logger.try_emplace(logs_directory);
...
if (m_logger.has_value())
{
m_logger->log(message);
}
else
{
std::cerr << message << std::endl;
}
-- Ryan P. Nicholl Tel: (678)-358-7765
Received on 2021-06-15 01:37:53