Skip to content

-Dprivate=public in test CMakeLists.txt breaks std::any compilation with C++17 #3345

@Felix-Gong

Description

@Felix-Gong

Description

When building BRPC tests with C++17 (GCC 12+), compilation fails with:

error: 'struct std::any::_Manager_internal<_Tp>' redeclared with different access

The root cause is -Dprivate=public -Dprotected=public in test/CMakeLists.txt line 56, which is used to access private members in tests. This macro rewrites private to public in all headers, including standard library headers like <any>.

Steps to Reproduce

# On GCC 12+ with C++17
g++ -std=c++17 -Dprivate=public -Dprotected=public -c test.cpp
# where test.cpp transitively includes <any> (e.g. via gtest-port.h)

The <any> header is pulled in by gtest's gtest-port.h:2312:

#if __has_include(<any>) && __cplusplus >= 201703L
#include <any>
#endif

Environment

  • GCC 12+ (tested with GCC 13.3 on x86, GCC 15.1 on RISC-V)
  • C++17 or later
  • Any architecture (x86, ARM, RISC-V)

Root Cause

test/CMakeLists.txt line 56:

set(CMAKE_CPP_FLAGS "${CMAKE_CPP_FLAGS} ... -Dprivate=public -Dprotected=public -include ${PROJECT_SOURCE_DIR}/test/sstream_workaround.h")

The existing sstream_workaround.h handles the same issue for <sstream> by including it before the macro takes effect, but does not protect <any>.

Why CI Does Not Catch This

BRPC's CMakeLists.txt hardcodes -std=c++11 (line 155/158), so __cplusplus is 201103L and gtest skips the #include <any>. The bug only manifests when building with C++17 explicitly or when the compiler defaults to C++17 (e.g. GCC 14+).

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