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

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

@MarshalClow Ah, OK, thanks. It makes totally sense. Do you know if it is said somewhere that we could use <RETURN TYPE>(<FUNCTION NAME>) as an alternative syntax to <RETURN TYPE> <FUNCTION_NAME>?
There's a good example in this answer (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(<FUNCTION NAME>) is a valid syntax.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.