Search code examples
c++syntaxc++-chrono

Problems understanding the min and max declarations syntax in std::chrono::duration_values


I'm having a look at the piece of code below, from std::chrono, and I don't understand the _Rep(min)() syntax.

I know _Rep is the return type, and min has to be the method name, but then:

  • What's the difference between writing it like _Rep(min)() and _Rep min()?
  • And, why isn't the same done for zero?
template <class _Rep>
struct duration_values { // gets arithmetic properties of a type
    _NODISCARD static constexpr _Rep zero() noexcept {
        // get zero value
        return _Rep(0);
    }

    _NODISCARD static constexpr _Rep(min)() noexcept {
        // get smallest value
        return numeric_limits<_Rep>::lowest();
    }

    _NODISCARD static constexpr _Rep(max)() noexcept {
        // get largest value
        return (numeric_limits<_Rep>::max)();
    }
};

I've seen the same construct in numeric_limits:

    _NODISCARD static constexpr _Ty(min)() noexcept {
        return _Ty();
    }
    
    _NODISCARD static constexpr _Ty(max)() noexcept {
        return _Ty();
    }

More info, just in case it's needed:

  • I'm using Microsoft Visual Studio Professional 2022 Preview (64-bit), Version 17.0.0, Preview 1.1.
  • My chrono file is under Microsoft Visual Studio\2022\Preview\VC\Tools\MSVC\14.29.30130\include.

And a little online example I've been playing with: https://godbolt.org/z/E5nW7x8vW


Solution

  • Its to defeat macro expansion; the Windows headers used to define macros named min and max.