Является ли A::членом вектора варианта неопределенного поведения? [дубликат]C++

Программы на C++. Форум разработчиков
Ответить Пред. темаСлед. тема
Anonymous
 Является ли A::членом вектора варианта неопределенного поведения? [дубликат]

Сообщение Anonymous »

Код: Выделить всё

#include 
#include 

struct A;

using B = std::variant;

struct A {
std::vector v;
};

int main() {
A a;
}
Он компилируется с помощью GCC, clang и MSVC.
Когда я заменяю std::vector на std::optional это больше не работает.

Код: Выделить всё

#include 
#include 

struct A;

using B = std::variant;

struct A {
std::optional v;
};

int main() {
A a;
}
clang + libstdc++:

Код: Выделить всё

In file included from :1:
In file included from /opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:40:
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/type_traits:3416:7: error: incomplete type 'A' used in type trait expression
3416 |     = __is_trivially_constructible(_Tp, __add_rval_ref_t);
|       ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:372:5: note: in instantiation of variable template specialization 'std::is_trivially_move_constructible_v' requested here
372 |           (is_trivially_move_constructible_v && ...);
|            ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:377:23: note: in instantiation of static data member 'std::__detail::__variant::_Traits::_S_trivial_move_ctor' requested here
377 |           _S_trivial_dtor && _S_trivial_move_ctor
|                              ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:759:45: note: in instantiation of static data member 'std::__detail::__variant::_Traits::_S_trivial_move_assign' requested here
759 |       _Move_assign_base;
|                                             ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:762:28: note: in instantiation of template type alias '_Move_assign_alias' requested here
762 |     struct _Variant_base : _Move_assign_alias
|                            ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:1362:15: note: in instantiation of template class 'std::__detail::__variant::_Variant_base' requested here
1362 |     : private __detail::__variant::_Variant_base,
|               ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/type_traits:3413:7: note: in instantiation of template class 'std::variant' requested here
3413 |     = __is_trivially_constructible(_Tp, __add_lval_ref_t);
|       ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/optional:506:12: note: in instantiation of variable template specialization 'std::is_trivially_copy_constructible_v' requested here
506 |            bool = is_trivially_copy_constructible_v,
|                   ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/optional:704:15: note: in instantiation of default argument for '_Optional_base' required here
704 |     : private _Optional_base,
|               ^~~~~~~~~~~~~~~~~~~
:9:22: note: in instantiation of template class 'std::optional' requested here
9 |     std::optional  v;
|                      ^
:8:8: note: definition of 'A' is not complete until the closing '}'
8 | struct A {
|        ^
In file included from :1:
In file included from /opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:40:
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/type_traits:3413:7: error: incomplete type 'A' used in type trait expression
3413 |     = __is_trivially_constructible(_Tp, __add_lval_ref_t);
|       ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:370:5: note: in instantiation of variable template specialization 'std::is_trivially_copy_constructible_v' requested here
370 |           (is_trivially_copy_constructible_v && ...);
|            ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:374:23: note: in instantiation of static data member 'std::__detail::__variant::_Traits::_S_trivial_copy_ctor' requested here
374 |           _S_trivial_dtor && _S_trivial_copy_ctor
|                              ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:705:45: note: in instantiation of static data member 'std::__detail::__variant::_Traits::_S_trivial_copy_assign' requested here
705 |       _Copy_assign_base;
|                                             ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:708:32: note: in instantiation of template type alias '_Copy_assign_alias' requested here
708 |     struct _Move_assign_base : _Copy_assign_alias
|                                ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:762:28: note: in instantiation of template class 'std::__detail::__variant::_Move_assign_base' requested here
762 |     struct _Variant_base : _Move_assign_alias
|                            ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:1362:15: note: in instantiation of template class 'std::__detail::__variant::_Variant_base' requested here
1362 |     : private __detail::__variant::_Variant_base,
|               ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/type_traits:3413:7: note: in instantiation of template class 'std::variant' requested here
3413 |     = __is_trivially_constructible(_Tp, __add_lval_ref_t);
|       ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/optional:506:12: note: in instantiation of variable template specialization 'std::is_trivially_copy_constructible_v' requested here
506 |            bool = is_trivially_copy_constructible_v,
|                   ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/optional:704:15: note: in instantiation of default argument for '_Optional_base' required here
704 |     : private _Optional_base,
|               ^~~~~~~~~~~~~~~~~~~
:9:22: note: in instantiation of template class 'std::optional' requested here
9 |     std::optional  v;
|                      ^
:8:8: note: definition of 'A' is not complete until the closing '}'
8 | struct A {
|        ^
In file included from :1:
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:276:8: error: field has incomplete type 'A'
276 |         _Type _M_storage;
|               ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:274:7: note: in instantiation of member class 'std::__detail::__variant::_Uninitialized::(anonymous union at /opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:274:7)' requested here
274 |       union {
|       ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:434:30: note: in instantiation of template class 'std::__detail::__variant::_Uninitialized' requested here
434 |       _Uninitialized _M_first;
|                              ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:516:41: note: in instantiation of template class 'std::__detail::__variant::_Variadic_union' requested here
516 |       _Variadic_union _M_u;
|                                         ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:581:30: note: in instantiation of template class 'std::__detail::__variant::_Variant_storage' requested here
581 |     struct _Copy_ctor_base : _Variant_storage_alias
|                              ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:618:30: note: in instantiation of template class 'std::__detail::__variant::_Copy_ctor_base' requested here
618 |     struct _Move_ctor_base : _Copy_ctor_alias
|                              ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:656:32: note: (skipping 3 contexts in backtrace; use -ftemplate-backtrace-limit=0 to see all)
656 |     struct _Copy_assign_base : _Move_ctor_alias
|                                ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:1362:15: note: in instantiation of template class 'std::__detail::__variant::_Variant_base' requested here
1362 |     : private __detail::__variant::_Variant_base,
|               ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/type_traits:3413:7: note: in instantiation of template class 'std::variant' requested here
3413 |     = __is_trivially_constructible(_Tp, __add_lval_ref_t);
|       ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/optional:506:12: note: in instantiation of variable template specialization 'std::is_trivially_copy_constructible_v' requested here
506 |            bool = is_trivially_copy_constructible_v,
|                   ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/optional:704:15: note: in instantiation of default argument for '_Optional_base' required here
704 |     : private _Optional_base,
|               ^~~~~~~~~~~~~~~~~~~
:9:22: note: in instantiation of template class 'std::optional' requested here
9 |     std::optional  v;
|                      ^
:8:8: note: definition of 'A' is not complete until the closing '}'
8 | struct A {
|        ^
In file included from :1:
In file included from /opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:40:
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/type_traits:3388:7: error: incomplete type 'A' used in type trait expression
3388 |     = __is_constructible(_Tp, __add_lval_ref_t);
|       ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:357:5: note: in instantiation of variable template specialization 'std::is_copy_constructible_v' requested here
357 |           (is_copy_constructible_v && ...);
|            ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:1364:43: note: in instantiation of static data member 'std::__detail::__variant::_Traits::_S_copy_ctor' requested here
1364 |         __detail::__variant::_Traits::_S_copy_ctor,
|                                                  ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/type_traits:3413:7: note: in instantiation of template class 'std::variant' requested here
3413 |     = __is_trivially_constructible(_Tp, __add_lval_ref_t);
|       ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/optional:506:12: note: in instantiation of variable template specialization 'std::is_trivially_copy_constructible_v' requested here
506 |            bool = is_trivially_copy_constructible_v,
|                   ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/optional:704:15: note: in instantiation of default argument for '_Optional_base' required here
704 |     : private _Optional_base,
|               ^~~~~~~~~~~~~~~~~~~
:9:22: note: in instantiation of template class 'std::optional' requested here
9 |     std::optional v;
|                      ^
:8:8: note: definition of 'A' is not complete until the closing '}'
8 | struct A {
|        ^
In file included from :1:
In file included from /opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:40:
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/type_traits:3391:7: error: incomplete type 'A' used in type trait expression
3391 |     = __is_constructible(_Tp, __add_rval_ref_t);
|       ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:359:5: note: in instantiation of variable template specialization 'std::is_move_constructible_v' requested here
359 |           (is_move_constructible_v &&  ...);
|            ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:1366:43: note: in instantiation of static data member 'std::__detail::__variant::_Traits::_S_move_ctor' requested here
1366 |         __detail::__variant::_Traits::_S_move_ctor,
|                                                  ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/type_traits:3413:7: note: in instantiation of template class 'std::variant' requested here
3413 |     = __is_trivially_constructible(_Tp, __add_lval_ref_t);
|       ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/optional:506:12: note: in instantiation of variable template specialization 'std::is_trivially_copy_constructible_v' requested here
506 |            bool = is_trivially_copy_constructible_v,
|                   ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/optional:704:15: note: in instantiation of default argument for '_Optional_base' required here
704 |     : private _Optional_base,
|               ^~~~~~~~~~~~~~~~~~~
:9:22: note: in instantiation of template class 'std::optional' requested here
9 |     std::optional v;
|                      ^
:8:8: note: definition of 'A' is not complete until the closing '}'
8 | struct A {
|        ^
In file included from :1:
In file included from /opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:40:
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/type_traits:3385:54: error: incomplete type 'A' used in type trait expression
3385 |   inline constexpr bool is_default_constructible_v = __is_constructible(_Tp);
|                                                      ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:1422:26: note: in instantiation of variable template specialization 'std::is_default_constructible_v' requested here
1422 |       variant() requires is_default_constructible_v = default;
|                          ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:1422:26: note: while substituting template arguments into constraint expression here
1422 |       variant() requires is_default_constructible_v = default;
|                          ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/variant:1422:7: note: while checking constraint satisfaction for function 'variant' required here
1422 |       variant() requires is_default_constructible_v = default;
|       ^~~~~~~
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/type_traits:3413:7: note: in instantiation of template class 'std::variant' requested here
3413 |     = __is_trivially_constructible(_Tp, __add_lval_ref_t);
|       ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/optional:506:12: note: in instantiation of variable template specialization 'std::is_trivially_copy_constructible_v' requested here
506 |            bool = is_trivially_copy_constructible_v,
|                   ^
/opt/compiler-explorer/gcc-14.2.0/lib/gcc/x86_64-linux-gnu/14.2.0/../../../../include/c++/14.2.0/optional:704:15: note: in instantiation of default argument for '_Optional_base' required here
704 |     : private _Optional_base,
|               ^~~~~~~~~~~~~~~~~~~
:9:22: note: in instantiation of template class 'std::optional' requested here
9 |     std::optional v;
|                      ^
:8:8: note: definition of 'A' is not complete until the closing '}'
8 | struct A {
|        ^
Я полагаю, это работает, потому что все реализации std::vector хранят данные в куче. Но стандарт не требует этого, не так ли? Если я не ошибаюсь, возможно, некоторые реализации std::vector выполняют некоторую небольшую оптимизацию данных, делая его похожим на std::optional.


Подробнее здесь: https://stackoverflow.com/questions/790 ... -behaviour
Реклама
Ответить Пред. темаСлед. тема

Быстрый ответ

Изменение регистра текста: 
Смайлики
:) :( :oops: :roll: :wink: :muza: :clever: :sorry: :angel: :read: *x)
Ещё смайлики…
   
К этому ответу прикреплено по крайней мере одно вложение.

Если вы не хотите добавлять вложения, оставьте поля пустыми.

Максимально разрешённый размер вложения: 15 МБ.

  • Похожие темы
    Ответы
    Просмотры
    Последнее сообщение

Вернуться в «C++»