Requirements: min and max macros

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
8 messages Options
Reply | Threaded
Open this post in threaded view
|

Requirements: min and max macros

Vinnie Falco
From
http://www.boost.org/development/requirements.html#Naming

"Make sure your code compiles in the presence of the min() and max()
macros. Some platform headers define min() and max()macros which cause
some common C++ constructs to fail to compile. Some simple tricks can
protect your code from inappropriate macro substitution:..."

There is one case not addressed. What to do with this:

    std::min<std::size_t>(a, b);

?

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: Requirements: min and max macros

Andrey Semashev-2
On 02/05/17 20:15, Vinnie Falco wrote:

> From
> http://www.boost.org/development/requirements.html#Naming
>
> "Make sure your code compiles in the presence of the min() and max()
> macros. Some platform headers define min() and max()macros which cause
> some common C++ constructs to fail to compile. Some simple tricks can
> protect your code from inappropriate macro substitution:..."
>
> There is one case not addressed. What to do with this:
>
>     std::min<std::size_t>(a, b);
>
> ?

min and max are function-like macros. The code above is not affected by
those.


_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: Requirements: min and max macros

Jonathan Müller
In reply to this post by Vinnie Falco
On 05.02.2017 18:15, Vinnie Falco wrote:
> There is one case not addressed. What to do with this:
>
>     std::min<std::size_t>(a, b);
>
> ?
(std::min)(std::size_t(a), std::size_t(b))

?

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: Requirements: min and max macros

Bjorn Reese
In reply to this post by Vinnie Falco
On 02/05/2017 06:15 PM, Vinnie Falco wrote:
> From
> http://www.boost.org/development/requirements.html#Naming
>
> "Make sure your code compiles in the presence of the min() and max()
> macros. Some platform headers define min() and max()macros which cause
> some common C++ constructs to fail to compile. Some simple tricks can
> protect your code from inappropriate macro substitution:..."

Is this guideline still relevant?


_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: Requirements: min and max macros

Vinnie Falco
On Mon, Feb 6, 2017 at 6:39 AM, Bjorn Reese <[hidden email]> wrote:
> Is this guideline still relevant?

I believe so, at least for Microsoft Visual C++ if you have not
defined "NOMINMAX".

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: Requirements: min and max macros

Gavin Lambert
On 7/02/2017 00:48, Vinnie Falco wrote:
> On Mon, Feb 6, 2017 at 6:39 AM, Bjorn Reese <[hidden email]> wrote:
>> Is this guideline still relevant?
>
> I believe so, at least for Microsoft Visual C++ if you have not
> defined "NOMINMAX".

If the *application author* has not defined NOMINMAX.  Libraries don't
get a choice in the matter, except for their own compiled source.



_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: Requirements: min and max macros

pabristow
In reply to this post by Bjorn Reese


> -----Original Message-----
> From: Boost [mailto:[hidden email]] On Behalf Of Bjorn Reese
> Sent: 06 February 2017 11:40
> To: [hidden email]
> Subject: Re: [boost] Requirements: min and max macros
>
> On 02/05/2017 06:15 PM, Vinnie Falco wrote:
> > From
> > http://www.boost.org/development/requirements.html#Naming
> >
> > "Make sure your code compiles in the presence of the min() and max()
> > macros. Some platform headers define min() and max()macros which cause
> > some common C++ constructs to fail to compile. Some simple tricks can
> > protect your code from inappropriate macro substitution:..."
>
> Is this guideline still relevant?

Yes - if only because the Boost checking tool  
http://www.boost.org/doc/libs/1_63_0/tools/inspect/index.html

will complain if it is missing.

http://www.boost.org/development/requirements.html tells you what to do - search down for max  or min.

"
Make sure your code compiles in the presence of the min() and max() macros. Some platform headers define min() and max() macros
which cause some common C++ constructs to fail to compile. Some simple tricks can protect your code from inappropriate macro
substitution:

    If you want to call std::min() or std::max():
        If you do not require argument-dependent look-up, use (std::min)(a,b).
        If you do require argument-dependent look-up, you should:
            #include <boost/config.hpp>
            Use BOOST_USING_STD_MIN(); to bring std::min() into the current scope.
            Use min BOOST_PREVENT_MACRO_SUBSTITUTION (a,b); to make an argument-dependent call to min(a,b).
    If you want to call std::numeric_limits<int>::max(), use (std::numeric_limits<int>::max)() instead.
    If you want to call a min() or max() member function, instead to doing obj.min(), use (obj.min)().
    If you want to declare or define a function or a member function named min or max, then you must use the
BOOST_PREVENT_MACRO_SUBSTITUTION macro. Instead of writing int min() { return 0; } you should write int min
BOOST_PREVENT_MACRO_SUBSTITUTION () { return 0; } This is true regardless if the function is a free (namespace scope) function, a
member function or a static member function, and it applies for the function declaration as well as for the function definition.
"

And anyway, it is a Good Thing because it will cause trouble to the many Microsoft users.

Paul

PS That this question was asked suggests that our tools for finding the right documentation are still lacking (or does this not
answer the original poster's question?)

---
Paul A. Bristow
Prizet Farmhouse
Kendal UK LA8 8AB
+44 (0) 1539 561830







_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: Requirements: min and max macros

Vinnie Falco
On Tue, Feb 7, 2017 at 11:27 AM, Paul A. Bristow
<[hidden email]> wrote:
> PS That this question was asked suggests that our tools for finding the right documentation are still lacking (or does this not answer the original poster's question?)

The tools are fine, its just that the docs didn't specifically address
the case where min and max are called with explicit template
arguments, e.g.:
    std::min<std::size_t>(x, y);

However, as Andrey pointed out:
>min and max are function-like macros. The code above is not affected by those.

That answers my question. It might be worth adding a sentence
describing this case to
http://www.boost.org/development/requirements.html to clarify.

Regards

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost