Skip to content

fix: include <any> in sstream_workaround.h to fix C++17 build#3346

Open
Felix-Gong wants to merge 1 commit into
apache:masterfrom
Felix-Gong:fix-std-any-cpp17
Open

fix: include <any> in sstream_workaround.h to fix C++17 build#3346
Felix-Gong wants to merge 1 commit into
apache:masterfrom
Felix-Gong:fix-std-any-cpp17

Conversation

@Felix-Gong

Copy link
Copy Markdown
Contributor

Summary

  • Fix std::any redeclaration error when building tests with C++17 and -Dprivate=public
  • The existing sstream_workaround.h already handles the same issue for <sstream>, but does not protect <any>

Fixes #3345

Test plan

  • Verified on x86 (GCC 13.3) and RISC-V (GCC 15.1) that the error reproduces with C++17 + -Dprivate=public
  • Verified the fix resolves the issue on both platforms
  • C++11/14 are unaffected (no std::any)

@chenBright chenBright left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using -fno-access-control, like in test/BUILD.bazel, would be more reasonable.

"-fno-access-control",

Comment thread test/sstream_workaround.h Outdated
# if __cplusplus >= 201703L
# include <any>
# endif
# define private public

@chenBright chenBright Jun 14, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using -fno-access-control instead of # define private public?

@Felix-Gong

Copy link
Copy Markdown
Contributor Author

Good point! -fno-access-control is indeed a cleaner approach than #define private public. It avoids the redeclaration issues with standard library headers entirely.

I'll update the PR to:

  1. Replace -Dprivate=public with -fno-access-control in CMake build flags
  2. Simplify sstream_workaround.h (the #ifdef private guard becomes unnecessary)

The Bazel build already uses -fno-access-control at test/BUILD.bazel:26, so this makes CMake consistent with Bazel.

@chenBright

Copy link
Copy Markdown
Contributor

test/Makefile also needs to be updated.

CPPFLAGS+=-DBTHREAD_USE_FAST_PTHREAD_MUTEX -D_GNU_SOURCE -DUSE_SYMBOLIZE -DNO_TCMALLOC -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -D__STDC_CONSTANT_MACROS -DUNIT_TEST -Dprivate=public -Dprotected=public -DBVAR_NOT_LINK_DEFAULT_VARIABLES --include sstream_workaround.h

Replace the -Dprivate=public -Dprotected=public macro hack with the
-fno-access-control compiler flag in both CMake and Makefile builds.
The macro approach caused redeclaration errors with standard library
headers (<sstream>, <any>, etc.) that use private access specifiers
internally.

-fno-access-control is a GCC/Clang compiler flag that cleanly disables
access control checking without affecting header parsing, eliminating
the need for the sstream_workaround.h workaround logic.

The Bazel build already uses -fno-access-control at test/BUILD.bazel,
so this makes CMake and Makefile builds consistent with Bazel.

Signed-off-by: Felix-Gong <gongxiaofei24@iscas.ac.cn>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

2 participants