Skip to content

"May be used uninitialized" warning from g++ 15.2.1 with -O2 #629

Open

Description

@Kirisummer

First of all, thanks for the library, it is very useful!

I included the library sources (reflectcpp.cpp, reflectcpp_json.cpp and yyjson.c) to my sources and tried building them unoptimized and optimized. Build succeeds without -O flag and with -O1, but some warnings appear with -O2.

Example code (main.cpp):

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

#include "rfl/json.hpp"
#include "rfl.hpp"

enum class Enum1 {
    Val1, Val2
};

enum class Enum2 {
    Val1, Val2
};

enum class Enum3 {
    Val1, Val2
};

struct Struct1 {
    Enum1 e1;
    Enum2 e2;
};

struct Struct2 {
    Enum3 e3;
};

using Variant = std::variant<Struct1, Struct2>;
using Vector = std::vector<Variant>;

int main() {
    Struct1 s1{Enum1::Val1, Enum2::Val2};
    Struct2 s2{Enum3::Val1};
    Vector vec{s1, s2};

    std::stringstream stream;
    rfl::json::write<rfl::AddTagsToVariants>(vec, stream);
    const auto res = rfl::json::read<Vector, rfl::AddTagsToVariants>(stream);
    if (res) {
        rfl::json::write<rfl::AddTagsToVariants>(res.value(), std::cout) << '\n';
    } else {
        std::cerr << "Failed to read Vector: " << res.error().what() << '\n';
    }
}

Compiler command:
g++ -Wall -std=c++23 -Werror main.cpp src/reflectcpp.cpp src/reflectcpp_json.cpp src/yyjson.c -I lib/reflect-cpp/include/ -I lib/reflect-cpp/include/rfl/thirdparty/ -O2 >& error.txt

Compiler output is very big, so I'm attaching a separate file: error.txt

reflect-cpp is checked out on tag v0.24.0, g++ version is 15.2.1

Activity

  1. liuzicheng1987 commented on Mar 8, 2026

    @liuzicheng1987
    Collaborator
    >::_Vector_impl_data::_M_end_of_storage’ may be used uninitialized [-Werror=maybe-uninitialized]
      113 |           _M_end_of_storage(__x._M_end_of_storage)
    

    @Kirisummer , this does not even appear to be from our library, this piece of code is from the C++ standard library. I also don't know how exactly Object<...>:i_ could be used uninitialized.

  2. liuzicheng1987 commented on Mar 8, 2026

    @liuzicheng1987
    Collaborator

    @Kirisummer , this is very weird, I'll have to take a closer look. Thanks for reporting.

  3. Kirisummer commented on Mar 8, 2026

    @Kirisummer
    Author

    The warnings happen on lines with creation of Generic from Array or Null, and Generic is immediately passed to vector::push_back. Some warnings say that is used before initialization. I suspect this might be the case where a temporary value is optimized out.

    I will try to create a variable which is then moved inside the vector, or to use emplace_back instead of push_back to avoid creating a temporary value at all.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions