Skip to content

[libc++] std::expected does not work properly with std::any #92676

@Hackerl

Description

@Hackerl

Compilation errors will occur when using types like std::expected<std::any, int>, but both gcc and msvc can compile normally.

for example: https://godbolt.org/z/nYaEafoah

#include <expected>
#include <any>
#include <memory>

int main() {
    auto core = std::make_shared<std::expected<std::any, int>>();
    return 0;
}

output:

In file included from <source>:1:
In file included from /opt/compiler-explorer/clang-18.1.0/bin/../include/c++/v1/expected:44:
/opt/compiler-explorer/clang-18.1.0/bin/../include/c++/v1/__expected/expected.h:540:14: error: satisfaction of constraint '__can_convert<_Up, _OtherErr, const _Up &, const _OtherErr &>::value' depends on itself
  540 |     requires __can_convert<_Up, _OtherErr, const _Up&, const _OtherErr&>::value
      |              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/clang-18.1.0/bin/../include/c++/v1/__expected/expected.h:540:14: note: while substituting template arguments into constraint expression here
  540 |     requires __can_convert<_Up, _OtherErr, const _Up&, const _OtherErr&>::value
      |              ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/clang-18.1.0/bin/../include/c++/v1/__type_traits/is_copy_constructible.h:25:38: note: while checking constraint satisfaction for template 'expected<std::any, int>' required here
   25 |     : public integral_constant<bool, __is_constructible(_Tp, __add_lvalue_reference_t<typename add_const<_Tp>::type>)> {
      |                                      ^~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/clang-18.1.0/bin/../include/c++/v1/__type_traits/is_copy_constructible.h:25:38: note: while substituting deduced template arguments into function template 'expected' [with _Up = std::any, _OtherErr = int]
/opt/compiler-explorer/clang-18.1.0/bin/../include/c++/v1/any:211:35: note: in instantiation of template class 'std::is_copy_constructible<std::expected<std::any, int>>' requested here
  211 |                                   is_copy_constructible<_Tp>::value> >
      |                                   ^
/opt/compiler-explorer/clang-18.1.0/bin/../include/c++/v1/any:460:6: note: in instantiation of default argument for 'any<std::expected<std::any, int> &, decay_t<expected<any, int> &>>' required here
  460 | any::any(_ValueType&& __v) : __h_(nullptr) {
      |      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  461 |   __any_imp::_Handler<_Tp>::__create(*this, std::forward<_ValueType>(__v));
      |   ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  462 | }
      | ~
/opt/compiler-explorer/clang-18.1.0/bin/../include/c++/v1/__type_traits/is_constructible.h:22:103: note: (skipping 11 contexts in backtrace; use -ftemplate-backtrace-limit=0 to see all)
   22 | struct _LIBCPP_TEMPLATE_VIS is_constructible : public integral_constant<bool, __is_constructible(_Tp, _Args...)> {};
      |                                                                                                       ^
/opt/compiler-explorer/clang-18.1.0/bin/../include/c++/v1/__memory/shared_ptr.h:306:10: note: in instantiation of template class 'std::__compressed_pair<std::allocator<std::expected<std::any, int>>, std::expected<std::any, int>>' requested here
  306 |   struct _ALIGNAS_TYPE(_CompressedPair) _Storage {
      |          ^
/opt/compiler-explorer/clang-18.1.0/bin/../include/c++/v1/__config:617:30: note: expanded from macro '_ALIGNAS_TYPE'
  617 | #    define _ALIGNAS_TYPE(x) alignas(x)
      |                              ^
/opt/compiler-explorer/clang-18.1.0/bin/../include/c++/v1/__memory/shared_ptr.h:325:17: note: in instantiation of member class 'std::__shared_ptr_emplace<std::expected<std::any, int>, std::allocator<std::expected<std::any, int>>>::_Storage' requested here
  325 |   static_assert(_LIBCPP_ALIGNOF(_Storage) == _LIBCPP_ALIGNOF(_CompressedPair), "");
      |                 ^
/opt/compiler-explorer/clang-18.1.0/bin/../include/c++/v1/__config:616:34: note: expanded from macro '_LIBCPP_ALIGNOF'
  616 | #    define _LIBCPP_ALIGNOF(_Tp) alignof(_Tp)
      |                                  ^
/opt/compiler-explorer/clang-18.1.0/bin/../include/c++/v1/__memory/shared_ptr.h:823:51: note: in instantiation of template class 'std::__shared_ptr_emplace<std::expected<std::any, int>, std::allocator<std::expected<std::any, int>>>' requested here
  823 |   ::new ((void*)std::addressof(*__guard.__get())) _ControlBlock(__a, std::forward<_Args>(__args)...);
      |                                                   ^
/opt/compiler-explorer/clang-18.1.0/bin/../include/c++/v1/__memory/shared_ptr.h:831:15: note: in instantiation of function template specialization 'std::allocate_shared<std::expected<std::any, int>, std::allocator<std::expected<std::any, int>>, void>' requested here
  831 |   return std::allocate_shared<_Tp>(allocator<_Tp>(), std::forward<_Args>(__args)...);
      |               ^
<source>:6:22: note: in instantiation of function template specialization 'std::make_shared<std::expected<std::any, int>, void>' requested here
    6 |     auto core = std::make_shared<std::expected<std::any, int>>();
      |                      ^
1 error generated.
Compiler returned: 1

Another example: https://godbolt.org/z/sfqPT7T1W

#include <expected>
#include <any>
#include <utility>

static std::expected<std::any, int> a;

std::expected<std::any, int> test() {
    return std::move(a);
}

int main() {
    test();
    return 0;
}

Metadata

Metadata

Assignees

Labels

libc++libc++ C++ Standard Library. Not GNU libstdc++. Not libc++abi.

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions