Initialize vector of variants results in long list of warnings

51 Views Asked by At

I wrote some code and with optimizer -O2 I got a lot of warnings while running with -O3 is silent! Also gcc trunk ( godbolt) compiles without warnings. Compiler/library bug or simply my code is wrong?

With the following code: see on godbolt

#include <variant>
#include <iostream>
#include <vector>

struct A { int integer; };
struct B { std::string text; };

void x() 
{
    std::vector< std::variant< A, B >> v;
    v.push_back(B{"Erstes" }); 
    v.push_back(A{1});
}

int main()
{
    x();
}

and compiled with:

g++   -g -Wall -pedantic -std=c++20   -O2  -Werror test_vector_of_pointer_variant.cpp

Compiler Version: gcc (GCC) 13.2.1 20231011 (Red Hat 13.2.1-4)

I got this warnings:

                 from /usr/include/c++/13/bits/locale_classes.h:40,
                 from /usr/include/c++/13/bits/ios_base.h:41,
                 from /usr/include/c++/13/ios:44,
                 from /usr/include/c++/13/ostream:40,
                 from /usr/include/c++/13/iostream:41,
                 from test_vector_of_pointer_variant.cpp:2:
In member function 'constexpr bool std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_M_is_local() const [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]',
    inlined from 'constexpr std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' at /usr/include/c++/13/bits/basic_string.h:670:23,
    inlined from 'constexpr B::B(B&&)' at test_vector_of_pointer_variant.cpp:6:8,
    inlined from 'constexpr std::__detail::__variant::_Uninitialized<_Type, false>::_Uninitialized(std::in_place_index_t<0>, _Args&& ...) [with _Args = {B}; _Type = B]' at /usr/include/c++/13/variant:249:4,
    inlined from 'constexpr std::__detail::__variant::_Variadic_union<_First, _Rest ...>::_Variadic_union(std::in_place_index_t<0>, _Args&& ...) [with _Args = {B}; _First = B; _Rest = {}]' at /usr/include/c++/13/variant:384:4,
    inlined from 'constexpr std::__detail::__variant::_Variadic_union<_First, _Rest ...>::_Variadic_union(std::in_place_index_t<_Np>, _Args&& ...) [with long unsigned int _Np = 1; _Args = {B}; _First = A; _Rest = {B}]' at /usr/include/c++/13/variant:390:4,
    inlined from 'constexpr void std::_Construct(_Tp*, _Args&& ...) [with _Tp = __detail::__variant::_Variadic_union<A, B>; _Args = {const in_place_index_t<1>&, B}]' at /usr/include/c++/13/bits/stl_construct.h:119:7,
    inlined from 'std::__detail::__variant::_Move_ctor_base<false, A, B>::_Move_ctor_base(std::__detail::__variant::_Move_ctor_base<false, A, B>&&)::<lambda(auto:6&&, auto:7)> mutable [with auto:6 = B; auto:7 = std::integral_constant<long unsigned int, 1>]' at /usr/include/c++/13/variant:604:23,
    inlined from 'constexpr _Res std::__invoke_impl(__invoke_other, _Fn&&, _Args&& ...) [with _Res = void; _Fn = __detail::__variant::_Move_ctor_base<false, A, B>::_Move_ctor_base(std::__detail::__variant::_Move_ctor_base<false, A, B>&&)::<lambda(auto:6&&, auto:7)>; _Args = {B, integral_constant<long unsigned int, 1>}]' at /usr/include/c++/13/bits/invoke.h:61:36,
    inlined from 'constexpr typename std::__invoke_result<_Functor, _ArgTypes>::type std::__invoke(_Callable&&, _Args&& ...) [with _Callable = __detail::__variant::_Move_ctor_base<false, A, B>::_Move_ctor_base(std::__detail::__variant::_Move_ctor_base<false, A, B>&&)::<lambda(auto:6&&, auto:7)>; _Args = {B, integral_constant<long unsigned int, 1>}]' at /usr/include/c++/13/bits/invoke.h:96:40,
    inlined from 'static constexpr decltype(auto) std::__detail::__variant::__gen_vtable_impl<std::__detail::__variant::_Multi_array<_Result_type (*)(_Visitor, _Variants ...)>, std::integer_sequence<long unsigned int, __indices ...> >::__visit_invoke(_Visitor&&, _Variants ...) [with _Result_type = std::__detail::__variant::__variant_idx_cookie; _Visitor = std::__detail::__variant::_Move_ctor_base<false, A, B>::_Move_ctor_base(std::__detail::__variant::_Move_ctor_base<false, A, B>&&)::<lambda(auto:6&&, auto:7)>&&; _Variants = {std::variant<A, B>&&}; long unsigned int ...__indices = {1}]' at /usr/include/c++/13/variant:1021:17,
    inlined from 'constexpr decltype(auto) std::__do_visit(_Visitor&&, _Variants&& ...) [with _Result_type = __detail::__variant::__variant_idx_cookie; _Visitor = __detail::__variant::_Move_ctor_base<false, A, B>::_Move_ctor_base(std::__detail::__variant::_Move_ctor_base<false, A, B>&&)::<lambda(auto:6&&, auto:7)>; _Variants = {variant<A, B>}]' at /usr/include/c++/13/variant:1792:5,
    inlined from 'constexpr decltype(auto) std::__do_visit(_Visitor&&, _Variants&& ...) [with _Result_type = __detail::__variant::__variant_idx_cookie; _Visitor = __detail::__variant::_Move_ctor_base<false, A, B>::_Move_ctor_base(std::__detail::__variant::_Move_ctor_base<false, A, B>&&)::<lambda(auto:6&&, auto:7)>; _Variants = {variant<A, B>}]' at /usr/include/c++/13/variant:1732:5,
    inlined from 'constexpr void std::__detail::__variant::__raw_idx_visit(_Visitor&&, _Variants&& ...) [with _Visitor = _Move_ctor_base<false, A, B>::_Move_ctor_base(std::__detail::__variant::_Move_ctor_base<false, A, B>&&)::<lambda(auto:6&&, auto:7)>; _Variants = {std::variant<A, B>}]' at /usr/include/c++/13/variant:183:44,
    inlined from 'constexpr std::__detail::__variant::_Move_ctor_base<<anonymous>, _Types>::_Move_ctor_base(std::__detail::__variant::_Move_ctor_base<<anonymous>, _Types>&&) [with bool <anonymous> = false; _Types = {A, B}]' at /usr/include/c++/13/variant:599:28,
    inlined from 'constexpr std::__detail::__variant::_Copy_assign_base<<anonymous>, _Types>::_Copy_assign_base(std::__detail::__variant::_Copy_assign_base<<anonymous>, _Types>&&) [with bool <anonymous> = false; _Types = {A, B}]' at /usr/include/c++/13/variant:664:7,
    inlined from 'constexpr std::__detail::__variant::_Move_assign_base<<anonymous>, _Types>::_Move_assign_base(std::__detail::__variant::_Move_assign_base<<anonymous>, _Types>&&) [with bool <anonymous> = false; _Types = {A, B}]' at /usr/include/c++/13/variant:718:7,
    inlined from 'constexpr std::__detail::__variant::_Variant_base<_Types>::_Variant_base(std::__detail::__variant::_Variant_base<_Types>&&) [with _Types = {A, B}]' at /usr/include/c++/13/variant:749:7,
    inlined from 'constexpr std::variant<_Types>::variant(std::variant<_Types>&&) [with _Types = {A, B}]' at /usr/include/c++/13/variant:1405:7,
    inlined from 'constexpr decltype (::new(void*(0)) _Tp) std::construct_at(_Tp*, _Args&& ...) [with _Tp = variant<A, B>; _Args = {variant<A, B>}]' at /usr/include/c++/13/bits/stl_construct.h:97:14,
    inlined from 'static constexpr void std::allocator_traits<std::allocator<_CharT> >::construct(allocator_type&, _Up*, _Args&& ...) [with _Up = std::variant<A, B>; _Args = {std::variant<A, B>}; _Tp = std::variant<A, B>]' at /usr/include/c++/13/bits/alloc_traits.h:539:21,
    inlined from 'constexpr std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {std::variant<A, B>}; _Tp = std::variant<A, B>; _Alloc = std::allocator<std::variant<A, B> >]' at /usr/include/c++/13/bits/vector.tcc:117:30,
    inlined from 'constexpr void std::vector<_Tp, _Alloc>::push_back(value_type&&) [with _Tp = std::variant<A, B>; _Alloc = std::allocator<std::variant<A, B> >]' at /usr/include/c++/13/bits/stl_vector.h:1296:21,
    inlined from 'void x()' at test_vector_of_pointer_variant.cpp:12:16:
/usr/include/c++/13/bits/basic_string.h:266:17: error: '*(const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*)((char*)&<unnamed> + offsetof(std::value_type, std::variant<A, B>::<unnamed>.std::__detail::__variant::_Variant_base<A, B>::<unnamed>.std::__detail::__variant::_Move_assign_base<false, A, B>::<unnamed>.std::__detail::__variant::_Copy_assign_base<false, A, B>::<unnamed>.std::__detail::__variant::_Move_ctor_base<false, A, B>::<unnamed>.std::__detail::__variant::_Copy_ctor_base<false, A, B>::<unnamed>.std::__detail::__variant::_Variant_storage<false, A, B>::_M_u)).std::__cxx11::basic_string<char>::_M_string_length' may be used uninitialized [-Werror=maybe-uninitialized]
  266 |             if (_M_string_length > _S_local_capacity)
      |                 ^~~~~~~~~~~~~~~~
test_vector_of_pointer_variant.cpp: In function 'void x()':
test_vector_of_pointer_variant.cpp:12:16: note: '<anonymous>' declared here
   12 |     v.push_back(A{1});
      |     ~~~~~~~~~~~^~~~~~
In member function 'constexpr bool std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::_M_is_local() const [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]',
    inlined from 'constexpr std::__cxx11::basic_string<_CharT, _Traits, _Alloc>::basic_string(std::__cxx11::basic_string<_CharT, _Traits, _Alloc>&&) [with _CharT = char; _Traits = std::char_traits<char>; _Alloc = std::allocator<char>]' at /usr/include/c++/13/bits/basic_string.h:670:23,
    inlined from 'constexpr B::B(B&&)' at test_vector_of_pointer_variant.cpp:6:8,
    inlined from 'constexpr std::__detail::__variant::_Uninitialized<_Type, false>::_Uninitialized(std::in_place_index_t<0>, _Args&& ...) [with _Args = {B}; _Type = B]' at /usr/include/c++/13/variant:249:4,
    inlined from 'constexpr std::__detail::__variant::_Variadic_union<_First, _Rest ...>::_Variadic_union(std::in_place_index_t<0>, _Args&& ...) [with _Args = {B}; _First = B; _Rest = {}]' at /usr/include/c++/13/variant:384:4,
    inlined from 'constexpr std::__detail::__variant::_Variadic_union<_First, _Rest ...>::_Variadic_union(std::in_place_index_t<_Np>, _Args&& ...) [with long unsigned int _Np = 1; _Args = {B}; _First = A; _Rest = {B}]' at /usr/include/c++/13/variant:390:4,
    inlined from 'constexpr void std::_Construct(_Tp*, _Args&& ...) [with _Tp = __detail::__variant::_Variadic_union<A, B>; _Args = {const in_place_index_t<1>&, B}]' at /usr/include/c++/13/bits/stl_construct.h:119:7,
    inlined from 'std::__detail::__variant::_Move_ctor_base<false, A, B>::_Move_ctor_base(std::__detail::__variant::_Move_ctor_base<false, A, B>&&)::<lambda(auto:6&&, auto:7)> mutable [with auto:6 = B; auto:7 = std::integral_constant<long unsigned int, 1>]' at /usr/include/c++/13/variant:604:23,
    inlined from 'constexpr _Res std::__invoke_impl(__invoke_other, _Fn&&, _Args&& ...) [with _Res = void; _Fn = __detail::__variant::_Move_ctor_base<false, A, B>::_Move_ctor_base(std::__detail::__variant::_Move_ctor_base<false, A, B>&&)::<lambda(auto:6&&, auto:7)>; _Args = {B, integral_constant<long unsigned int, 1>}]' at /usr/include/c++/13/bits/invoke.h:61:36,
    inlined from 'constexpr typename std::__invoke_result<_Functor, _ArgTypes>::type std::__invoke(_Callable&&, _Args&& ...) [with _Callable = __detail::__variant::_Move_ctor_base<false, A, B>::_Move_ctor_base(std::__detail::__variant::_Move_ctor_base<false, A, B>&&)::<lambda(auto:6&&, auto:7)>; _Args = {B, integral_constant<long unsigned int, 1>}]' at /usr/include/c++/13/bits/invoke.h:96:40,
    inlined from 'static constexpr decltype(auto) std::__detail::__variant::__gen_vtable_impl<std::__detail::__variant::_Multi_array<_Result_type (*)(_Visitor, _Variants ...)>, std::integer_sequence<long unsigned int, __indices ...> >::__visit_invoke(_Visitor&&, _Variants ...) [with _Result_type = std::__detail::__variant::__variant_idx_cookie; _Visitor = std::__detail::__variant::_Move_ctor_base<false, A, B>::_Move_ctor_base(std::__detail::__variant::_Move_ctor_base<false, A, B>&&)::<lambda(auto:6&&, auto:7)>&&; _Variants = {std::variant<A, B>&&}; long unsigned int ...__indices = {1}]' at /usr/include/c++/13/variant:1021:17,
    inlined from 'constexpr decltype(auto) std::__do_visit(_Visitor&&, _Variants&& ...) [with _Result_type = __detail::__variant::__variant_idx_cookie; _Visitor = __detail::__variant::_Move_ctor_base<false, A, B>::_Move_ctor_base(std::__detail::__variant::_Move_ctor_base<false, A, B>&&)::<lambda(auto:6&&, auto:7)>; _Variants = {variant<A, B>}]' at /usr/include/c++/13/variant:1792:5,
    inlined from 'constexpr decltype(auto) std::__do_visit(_Visitor&&, _Variants&& ...) [with _Result_type = __detail::__variant::__variant_idx_cookie; _Visitor = __detail::__variant::_Move_ctor_base<false, A, B>::_Move_ctor_base(std::__detail::__variant::_Move_ctor_base<false, A, B>&&)::<lambda(auto:6&&, auto:7)>; _Variants = {variant<A, B>}]' at /usr/include/c++/13/variant:1732:5,
    inlined from 'constexpr void std::__detail::__variant::__raw_idx_visit(_Visitor&&, _Variants&& ...) [with _Visitor = _Move_ctor_base<false, A, B>::_Move_ctor_base(std::__detail::__variant::_Move_ctor_base<false, A, B>&&)::<lambda(auto:6&&, auto:7)>; _Variants = {std::variant<A, B>}]' at /usr/include/c++/13/variant:183:44,
    inlined from 'constexpr std::__detail::__variant::_Move_ctor_base<<anonymous>, _Types>::_Move_ctor_base(std::__detail::__variant::_Move_ctor_base<<anonymous>, _Types>&&) [with bool <anonymous> = false; _Types = {A, B}]' at /usr/include/c++/13/variant:599:28,
    inlined from 'constexpr std::__detail::__variant::_Copy_assign_base<<anonymous>, _Types>::_Copy_assign_base(std::__detail::__variant::_Copy_assign_base<<anonymous>, _Types>&&) [with bool <anonymous> = false; _Types = {A, B}]' at /usr/include/c++/13/variant:664:7,
    inlined from 'constexpr std::__detail::__variant::_Move_assign_base<<anonymous>, _Types>::_Move_assign_base(std::__detail::__variant::_Move_assign_base<<anonymous>, _Types>&&) [with bool <anonymous> = false; _Types = {A, B}]' at /usr/include/c++/13/variant:718:7,
    inlined from 'constexpr std::__detail::__variant::_Variant_base<_Types>::_Variant_base(std::__detail::__variant::_Variant_base<_Types>&&) [with _Types = {A, B}]' at /usr/include/c++/13/variant:749:7,
    inlined from 'constexpr std::variant<_Types>::variant(std::variant<_Types>&&) [with _Types = {A, B}]' at /usr/include/c++/13/variant:1405:7,
    inlined from 'constexpr decltype (::new(void*(0)) _Tp) std::construct_at(_Tp*, _Args&& ...) [with _Tp = variant<A, B>; _Args = {variant<A, B>}]' at /usr/include/c++/13/bits/stl_construct.h:97:14,
    inlined from 'static constexpr void std::allocator_traits<std::allocator<_CharT> >::construct(allocator_type&, _Up*, _Args&& ...) [with _Up = std::variant<A, B>; _Args = {std::variant<A, B>}; _Tp = std::variant<A, B>]' at /usr/include/c++/13/bits/alloc_traits.h:539:21,
    inlined from 'constexpr std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {std::variant<A, B>}; _Tp = std::variant<A, B>; _Alloc = std::allocator<std::variant<A, B> >]' at /usr/include/c++/13/bits/vector.tcc:117:30,
    inlined from 'constexpr void std::vector<_Tp, _Alloc>::push_back(value_type&&) [with _Tp = std::variant<A, B>; _Alloc = std::allocator<std::variant<A, B> >]' at /usr/include/c++/13/bits/stl_vector.h:1296:21,
    inlined from 'void x()' at test_vector_of_pointer_variant.cpp:12:16:
/usr/include/c++/13/bits/basic_string.h:266:17: error: '*(const std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*)((char*)&<unnamed> + offsetof(std::value_type, std::variant<A, B>::<unnamed>.std::__detail::__variant::_Variant_base<A, B>::<unnamed>.std::__detail::__variant::_Move_assign_base<false, A, B>::<unnamed>.std::__detail::__variant::_Copy_assign_base<false, A, B>::<unnamed>.std::__detail::__variant::_Move_ctor_base<false, A, B>::<unnamed>.std::__detail::__variant::_Copy_ctor_base<false, A, B>::<unnamed>.std::__detail::__variant::_Variant_storage<false, A, B>::_M_u)).std::__cxx11::basic_string<char>::_M_string_length' may be used uninitialized [-Werror=maybe-uninitialized]
  266 |             if (_M_string_length > _S_local_capacity)
      |                 ^~~~~~~~~~~~~~~~
test_vector_of_pointer_variant.cpp: In function 'void x()':
test_vector_of_pointer_variant.cpp:12:16: note: '<anonymous>' declared here
   12 |     v.push_back(A{1});
      |     ~~~~~~~~~~~^~~~~~
0

There are 0 best solutions below