I am trying to use an std::optional
to late instantiate an object (which is not valid before). I found an annoying situation, where I do not know how to elegantly solve this.
I have the following data structure:
struct Foo {
int foo1;
float foo2;
};
as a member std::optional<Foo> foo_
.
In a function
void Bar::bar(int const value1, float const value2) {
foo_.emplace(value1, value2);
}
To my surprise, this fails to compile (in GCC 7.1) because it tries to call the constructor of Foo
with int const&, float const&
.
Now naive me tried to specialize emplace
as:
foo_.emplace<int, float>(value1, value2);
which did not work either because it tries to use initializer_list
then.
So my question is how does one call emplace elegantly?