Skip to content

MSVC 2022 build fails: C compound literals in C++ sources (C4576) + narrowing init in ScoreOptions (C2397) #196

@BeneOblivion-byte

Description

@BeneOblivion-byte

Environment

  • OS: Windows (MSVC toolchain)
  • Compiler: MSVC 2022
  • Generator: Ninja
  • CMake: (any recent version; I used CMake-based build integrating this repo as sources)

Problem 1: Non-standard C compound literal syntax used in C++ ((Vec3){...}) triggers MSVC error C4576

MSVC rejects C compound literals in C++ mode. There are multiple occurrences like:

return (Vec3){r, g, b};

This produces (example):

  • cam/hct_solver.cc:281: error: C4576: a parenthesized type followed by an initializer list is a non-standard explicit type conversion syntax

Vec3 is defined as an aggregate type:

struct Vec3 {
  double a = 0.0;
  double b = 0.0;
  double c = 0.0;
};
Possible fix

Replace C compound literals with standard C++ aggregate initialization, e.g.

  • return (Vec3){r, g, b}; -> return Vec3{r, g, b};
  • return (Vec3){-1.0, -1.0, -1.0}; -> return Vec3{-1.0, -1.0, -1.0};

This should be portable across MSVC/Clang/GCC.

Problem 2: Narrowing conversion in ScoreOptions default member initializer triggers MSVC error C2397

In cpp/score/score.h:

struct ScoreOptions {
  size_t desired = 4;
  int fallback_color_argb = 0xff4285f4;  // Google Blue.
  bool filter = true;
};

MSVC reports:

  • cpp/score/score.h:39: error: C2397: conversion from 'unsigned int' to 'int' requires a narrowing conversion
Possible fix

Use an unsigned 32-bit type for ARGB (or use the existing Argb typedef if applicable), e.g.

  • Argb fallback_color_argb = 0xff4285f4u;
    or
  • uint32_t fallback_color_argb = 0xff4285f4u;

(Using int for ARGB values that may exceed INT_MAX seems problematic on its own.)

Question

Is MSVC intended to be supported for the C++ implementation? If yes, I’m happy to send a PR that replaces (Vec3){...} with Vec3{...} and adjusts the fallback_color_argb type to avoid narrowing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions