0

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

rturrado
  • 7,699
  • 6
  • 42
  • 62

1 Answers1

3

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

Marshall Clow
  • 15,972
  • 2
  • 29
  • 45
  • @MarshalClow Ah, OK, thanks. It makes totally sense. Do you know if it is said somewhere that we could use `()` as an alternative syntax to ` `? – rturrado Aug 14 '21 at 20:15
  • There's a good example in this answer (https://stackoverflow.com/a/650711/260313), and a good explanation from Johannes Schaub in a comment to that answer. Function-like macros need `(` as next token to the function name for a successful replacement. What I don't understand yet is wy `RETURN TYPE()` is a valid syntax. – rturrado Aug 15 '21 at 18:48