Skip to content

Latest commit

 

History

History
69 lines (47 loc) · 2.64 KB

File metadata and controls

69 lines (47 loc) · 2.64 KB

Agent-Assisted Code Improvements

This document tracks code improvements made with the assistance of AI agents.

Note: For compilation instructions, see README.md.

MSVC Compilation Error Fix - numeric_limits::max() Macro Conflict

Commit: 2e387a11
Date: 2026-04-06
File: src/pcm-sensor-server.cpp

Issue

MSVC compilation error caused by macro conflict with std::numeric_limits<T>::max()

The error occurred at two locations in the argument parsing code (the -p port and -D debug level options):

// Original code (problematic)
if ( val > std::numeric_limits<unsigned short>::max() )
    throw std::out_of_range( "port out of range" );

Root Cause

Windows headers (particularly windows.h and related headers) often define max and min as preprocessor macros:

#define max(a,b) (((a) > (b)) ? (a) : (b))

When the preprocessor encounters std::numeric_limits<unsigned short>::max(), the max() part gets expanded as a macro, resulting in malformed code that fails to compile.

Solution

Wrap the function name in parentheses to prevent macro expansion:

// Fixed code
if ( val > (std::numeric_limits<unsigned short>::max)() )
    throw std::out_of_range( "port out of range" );

By adding parentheses around max, the preprocessor no longer recognizes it as a macro invocation, allowing the actual member function to be called.

Technical Details

  • Affected code: Port validation (-p option) and debug level validation (-D option)
  • Context: Command-line argument parsing for -p (port) and -D (debug level) options
  • Alternative solutions:
    1. #undef max before use (not recommended, affects entire translation unit)
    2. Define NOMINMAX before including Windows headers (project-wide change)
    3. Use (std::numeric_limits<T>::max)() (chosen solution - local and surgical)

Why This Approach

The parentheses-wrapping approach is preferred because:

  1. Minimal impact: Only affects the specific call sites
  2. No side effects: Doesn't disable macros for other code that might depend on them
  3. Clear intent: Documents that macro expansion is being avoided
  4. Standard practice: Commonly used pattern in cross-platform C++ code

Best Practices

When writing portable C++ code that may be compiled with Windows headers:

  • Always wrap standard library function names like min, max in parentheses when calling them
  • Consider using (std::min)(a, b) and (std::max)(a, b) for function calls, and (std::numeric_limits<T>::max)() for numeric_limits
  • Be aware that macros from platform headers can interfere with identically-named functions