Skip to content

Fix C++ std::complex compatibility using standard C++ headers - #58

Merged
steppi merged 1 commit into
scipy:mainfrom
Emiliano-DM:fix/cpp-headers-clean
Sep 2, 2025
Merged

Fix C++ std::complex compatibility using standard C++ headers#58
steppi merged 1 commit into
scipy:mainfrom
Emiliano-DM:fix/cpp-headers-clean

Conversation

@Emiliano-DM

Copy link
Copy Markdown
Contributor

Summary

Fix C++ template compilation errors when including amos.h from C++ code by using standard C++ headers instead of problematic C headers.

Problem

Error: error: expected unqualified-id before '_Complex' in C++ files including amos.h

Root Cause:

  • #include <complex.h> defines macro #define complex _Complex
  • This breaks C++ template syntax: std::complex<double> becomes std::_Complex<double> (invalid)

Example failure:

// amos.h function declaration:
int acai(std::complex<double>, ...);  // Becomes: int acai(std::_Complex<double>, ...); ❌

Solution

Clean C++ standards approach:
- ✅ Replace #include <complex.h> with #include <complex>
- ✅ Replace conj() with std::conj() (17 instances)

Benefits

- ✅ Eliminates macro pollution - no more #define complex _Complex
- ✅ Standard C++ practice - uses proper <complex> header
- ✅ No downstream side effects - safe for code including this header
- ✅ Functionally identical - std::conj() behaves exactly like conj()
- ✅ Addresses reviewer concerns - avoids undefining standard macros in public headers

Changes

File: include/xsf/amos/amos.h
- Line 97: #include <complex.h> → #include <complex>
- 17 locations: conj(...) → std::conj(...)

Testing

✅ Direct C++ compilation test confirms template errors resolved✅ No functional changes - purely implementation improvement✅ Maintains full compatibility with existing function
signatures

Replace problematic C header approach with clean C++ solution:
- Change #include <complex.h> to #include <complex>
- Replace conj() calls with std::conj() (17 instances)

Benefits:
- No macro pollution (#define complex _Complex removed)
- Standard C++ practice
- No side effects on downstream code
- Functionally identical behavior

Fixes C++ template errors: 'expected unqualified-id before _Complex'

@WarrenWeckesser WarrenWeckesser left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Looks good to me.

@lucascolley
lucascolley requested a review from steppi August 30, 2025 20:50
@steppi

steppi commented Sep 2, 2025

Copy link
Copy Markdown
Member

Thanks @Emiliano-DM. For context, what happened here is that AMOS was translated from C to C++ very quickly to address the Windows build failures here scipy/scipy#20291 just before the 1.13 release. Basically, we went with the minimal diff that made the current test suite pass, with the idea that we'd come back and clean things up later. Thanks @WarrenWeckesser for reviewing.

@steppi
steppi merged commit 3a0b8a5 into scipy:main Sep 2, 2025
2 of 4 checks passed
JaRoSchm pushed a commit to JaRoSchm/xsf that referenced this pull request Nov 12, 2025
Replace problematic C header approach with clean C++ solution:
- Change #include <complex.h> to #include <complex>
- Replace conj() calls with std::conj() (17 instances)

Benefits:
- No macro pollution (#define complex _Complex removed)
- Standard C++ practice
- No side effects on downstream code
- Functionally identical behavior

Fixes C++ template errors: 'expected unqualified-id before _Complex'
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.

3 participants