【发布时间】:2021-08-26 14:38:46
【问题描述】:
在std::optional::emplace docs 中有一个接受std::initializer_list 的重载:
template< class U, class... Args >
T& emplace( std::initializer_list<U> ilist, Args&&... args );
前提是
std::is_constructible
::value 为真
我认为它可能用于放置 POD 类型,但显然这不是它的工作方式(在其他 SO 主题中,解释了 emplace 函数使用 () 语法而不是 {}):
struct A
{
int x;
int y;
int z;
};
int main()
{
A normalA{1, 2, 3}; // this is OK
std::cout << std::is_constructible<A, std::initializer_list<int>&, int, int, int>::value << std::endl; // false
std::cout << std::is_constructible<A, std::initializer_list<int>&>::value << std::endl; // false
std::optional<A> optA;
// optA.emplace({1, 2, 3}); // this is NOK
optA.emplace(A{1, 2, 3}); // I can walk it around with copy-ctor
}
我可以编写接受initializer_list的构造函数:
struct B
{
B(std::initializer_list<int> l) {/* some impl here */}
int x;
int y;
int z;
};
然后像这样调用emplace:
std::optional<B> optB;
optB.emplace({1, 2, 3});
但不应该首先emplace 重载T& emplace( Args&&... args ); 就足够了吗?
我认为它可能对数组类型有用,但std::optional<int[]> xxx; 无论如何都无法编译。
您能否提供一些使用第二个std::optional::emplace 重载的示例。
【问题讨论】:
-
可能使
emplace({1,2,3}, allocator);为optional<vector<int>>工作。即使没有分配器参数,也需要重载。
标签: c++ stdoptional