Skip to content

Modernize C++ to C++17: enum class, unique_ptr, constexpr, dead code removal#30

Merged
ardiloot merged 3 commits intomasterfrom
modernization3
Feb 18, 2026
Merged

Modernize C++ to C++17: enum class, unique_ptr, constexpr, dead code removal#30
ardiloot merged 3 commits intomasterfrom
modernization3

Conversation

@ardiloot
Copy link
Copy Markdown
Owner

Summary

Modernize the C++ codebase from C++11 to C++17 with corresponding Cython binding updates. All 601 tests pass.

Changes

Build system (setup.py)

  • Upgrade from -std=c++11 to -std=c++17 (Linux/macOS) and add /std:c++17 (Windows MSVC)
  • Guard -msse3 flag behind x86 architecture check for ARM compatibility
  • Remove force=True from cythonize()

C++ modernization (Common.h/cpp)

  • typedefusing aliases (dcomplex, Tensor3d, pairdd)
  • constconstexpr for compile-time constants; inline const for constI (non-literal type)
  • Convert 6 enums to enum class: WaveDirection, NonlinearProcess, Polarization, NonlinearTmmMode, TMMParam, TMMParamType
  • Add ENUM_CLASS_CYTHON_COMPAT macro providing operator|, operator<<, operator* for Cython interop
  • Move WlToOmega, OmegaToWl, sqr to constexpr/inline in header
  • Use const& for ApplyRotationMatrixToTensor parameters

C++ modernization (NonlinearLayer, NonlinearTMM, Waves, SecondOrderNLTMM)

  • Qualify all enum values with their enum class scope throughout
  • Use constexpr int iF/iB pattern for Array2cd indexing with enum class values
  • Convert 8 factory functions to return std::unique_ptr
  • Replace internal new/delete with stack allocation or make_unique
  • Replace NULL with nullptr throughout
  • Remove dead code: cerr before throw, break after return/throw

Bug fix

  • pB was never initialized in NonlinearLayer.cpp (line was duplicating pF = Vector3cd::Zero())

Cython updates (.pxd/.pyx)

  • Add unique_ptr import from libcpp.memory
  • Update all enum string aliases for enum class qualification
  • Add .release() to 8 unique_ptr call sites
  • Update 8 return type declarations to unique_ptr[T]

Line endings

  • Normalize all text files from CRLF to LF per .gitattributes

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request modernizes the C++ codebase from C++11 to C++17 standard with comprehensive updates to improve code quality and safety. The changes include upgrading compiler flags, converting traditional enums to enum classes, replacing raw pointers with unique_ptr, modernizing type aliases, and making appropriate use of constexpr. The PR also fixes a bug where pB was never initialized in NonlinearLayer.cpp and removes dead code. All 601 tests pass.

Changes:

  • Build system upgraded to C++17 with ARM compatibility improvements
  • Six enums converted to enum class with Cython interop macro
  • Eight factory functions converted to return std::unique_ptr for better memory safety
  • Type aliases modernized from typedef to using, constants upgraded to constexpr

Reviewed changes

Copilot reviewed 13 out of 49 changed files in this pull request and generated no comments.

Show a summary per file
File Description
setup.py Upgraded C++ standard to C++17, added ARM architecture detection for SSE3 flag, removed force=True from cythonize
Common.h/cpp Converted type aliases, constants to constexpr, enums to enum class, added ENUM_CLASS_CYTHON_COMPAT macro
NonlinearTMM.h/cpp Updated return types to unique_ptr, qualified enum values, removed dead code
NonlinearLayer.h/cpp Fixed pB initialization bug, qualified enum values with scope
Waves.h/cpp Converted WaveType to enum class, updated function signatures
SecondOrderNLTMM.h/cpp Updated return types to unique_ptr, qualified enum values
Material.h/cpp Line ending normalization only
CppSecondOrderNLTMM.pxd Added unique_ptr import, updated enum string aliases for enum class qualification
SecondOrderNLTMM.pyx Added .release() calls for unique_ptr returns
Python files Line ending normalization only
Test files Line ending normalization only
Documentation files Line ending normalization only

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@ardiloot ardiloot merged commit 63ccf1a into master Feb 18, 2026
10 checks passed
@ardiloot ardiloot deleted the modernization3 branch February 19, 2026 19:31
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.

2 participants