Skip to content

Compiler Warning: unary minus operator applied to unsigned type, result still unsigned #1913

@Sailanarmo

Description

@Sailanarmo

Godot version

4.6-stable

godot-cpp version

10.0.0-rc1

System information

Windows 11, Visual Studio 2022 Community Edition

Issue description

There are multiple warnings regarding unary minus operator applied to unsigned type, result still unsigned in multiple files.

This is due to the fact that there are integers being assigned a -2147483648 as their maximum value. See rendering_server.hpp line 814:

Image

According to this Stackoverflow Post, the - operator does a negation on the number 2147483648, which is 1 number over the maximum size of an integer.

I propose the solution is to either do as that Stackoverflow post does, if we do not want to add more of the standard library, and do:

static const int CANVAS_LAYER_MIN = -2147483647 - 1; // We subtract 1 because operator - performs negation on the number

Or if we are okay with it, introduce <limits> and use std::numeric_limits:

#include <limits>

static const int CANVAS_LAYER_MIN = std::numeric_limits<int>::min();
static const int CANVAS_LAYER_MAX = std::numeric_limits<int>::max();

If we want to be stricter with typing we can bring in <cstdlib>:

#include <limits>
#include <cstdlib>

static const std::int32_t CANVAS_LAYER_MIN = std::numeric_limits<std::int32_t>::min();
static const std::int32_t CANVAS_LAYER_MAX = std::numeric_limits<std::int32_t>::max();

Steps to reproduce

In a CMakeLists.txt project:

cmake_minimum_required(VERSION 3.26)

set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED True)

project(Test)

include(FetchContent)

FetchContent_Declare(
  GDExtension
  GIT_REPOSITORY https://github.com/godotengine/godot-cpp.git
  GIT_TAG 10.0.0-rc1
)

FetchContent_MakeAvailable(GDExtension)

add_executable(Test test.cpp)
target_link_libraries(Test PRIVATE godot::cpp)

Create an empty test.cpp file with anything in it. (Hello, World would probably suffice);

Run:

cmake -B build -S .
cmake --build build

And the warnings should show up in the terminal once the Godot-CPP project starts to compile.

Minimal reproduction project

I provided steps to reproduce the warnings.

You can also produce the warnings if you compile the following code with the /W4 flag turned on:

#include <cstdint>
#include <limits>

auto main() -> int
{
  static const int max_neg_value = -2147483648;
  static const int max_pos_value = 2147483647;

  static const std::int32_t max_neg_value32 = std::numeric_limits<std::int32_t>::min();
  static const std::int32_t max_pos_value32 = std::numeric_limits<std::int32_t>::max();

  return 0;
}

You will see the warning for only one of these and not the other.

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