Non-Obvious

Program 1:

#define min(a, b) ((a) < (b) ? (a) : (b))
struct S { static int const n = 0; };
int main() { return min(S::n, 1); }

Program 2:

#include <algorithm>
struct S { static int const n = 0; };
int main() { return std::min(S::n, 1); }

Program 3:

#include <algorithm>
struct S { static int const n = 0; };
int main() { return std::min({S::n, 1}); }

Where is the bug?

Right, program 2 lacks a definition of S::n.

std::min(T const &, T const &) takes its arguments by reference, so S::n does get odr-used there.

std::min(std::initializer_list<T>) does not have that gotcha.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s