Bug 101227 - Clang++ fails to instantiate std::optional if nested type has a non-static data member initializer
Summary: Clang++ fails to instantiate std::optional if nested type has a non-static da...
Status: NEW
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 11.1.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on: 96645
Blocks: NSDMI
  Show dependency treegraph
 
Reported: 2021-06-27 00:21 UTC by florin
Modified: 2024-02-21 16:51 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2021-11-22 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description florin 2021-06-27 00:21:07 UTC
I have reported https://bugs.llvm.org/show_bug.cgi?id=50904 and David suggested it might be a bug in libstdc++.

GCC11.1 compiles this ok, Clang11 fails:

#include <optional>

class Bar
{
public:
   struct Foo
   {
      int someInt = 3;
   };

   std::optional<Foo> theFoo;
};

int main()
{
   Bar aBar;

   aBar.theFoo = std::make_optional<Bar::Foo>();

   return 0;
}

---

<source>:18:18: error: no matching function for call to 'make_optional'
   aBar.theFoo = std::make_optional<Bar::Foo>();
                 ^~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/12.0.0/../../../../include/c++/12.0.0/optional:1276:5: note: candidate template ignored: requirement 'is_constructible_v<Bar::Foo>' was not satisfied [with _Tp = Bar::Foo, _Args = <>]
    make_optional(_Args&&... __args)
    ^
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/12.0.0/../../../../include/c++/12.0.0/optional:1268:5: note: candidate function template not viable: requires single argument '__t', but no arguments were provided
    make_optional(_Tp&& __t)
    ^
/opt/compiler-explorer/gcc-snapshot/lib/gcc/x86_64-linux-gnu/12.0.0/../../../../include/c++/12.0.0/optional:1284:5: note: candidate function template not viable: requires at least argument '__il', but no arguments were provided
    make_optional(initializer_list<_Up> __il, _Args&&... __args)
    ^
1 error generated.
Compiler returned: 1
Comment 1 Andrew Pinski 2021-06-27 04:24:35 UTC
clang is failing because of "requirement 'is_constructible_v<Bar::Foo>' was not satisfied [with _Tp = Bar::Foo, _Args = <>]"
But it seems like it should be true.  

from https://en.cppreference.com/w/cpp/types/is_constructible:
"If T is an object or reference type and the variable definition T obj(std::declval<Args>()...); is well-formed, provides the member constant value equal to true. In all other cases, value is false.
For the purposes of this check, the variable definition is never interpreted as a function declaration, and the use of std::declval is not considered an odr-use. "

So the question becomes why is std::is_constructible<Bar::Foo>::value is false.
Comment 2 David Blaikie 2021-06-27 16:05:37 UTC
My guess, libstdc++'s std::optional uses is_default_constructible unconditionally in some way, instantiating the template while the type parameter is incomplete (because the nested class is incomplete until the enclosing class is complete?).

Essentially something like this:

https://godbolt.org/z/6eohMofdb
Comment 3 Jonathan Wakely 2021-06-28 09:05:53 UTC
Yes. It's basically the same issue as PR 96645. Nested types and default member initializers are not compatible with standard library wrapper types. Yet another horrible corner of C++.