I have a construct similar to the following in my code.
#include <optional>
struct C {
struct C2 {
bool b { false };
};
std::optional<C2> oc2;
};
int main(int argc, char** argv) {
C c;
c.oc2.emplace(); // clang error!
}
It's an std::optional
object inside of a class that I want to initialize via its default constructor. The above code compiles fine in GCC 9. But Clang 9 fails with the following error:
% clang++-9 -std=c++17 -o main main.cpp
main.cpp:13:11: error: no matching member function for call to 'emplace'
c.oc2.emplace();
~~~~~~^~~~~~~
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/optional:845:2: note: candidate template ignored: requirement 'is_constructible_v<C::C2>' was not satisfied [with _Args = <>]
emplace(_Args&&... __args)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/optional:855:2: note: candidate function template not viable: requires at least argument '__il', but no arguments were provided
emplace(initializer_list<_Up> __il, _Args&&... __args)
^
1 error generated.
Is this a Clang bug or is there a better way to initialize an optional value in that case?