C++ Logo

std-proposals

Advanced search

Re: [std-proposals] Functions that don't need parentheses to make a call

From: Jason McKesson <jmckesson_at_[hidden]>
Date: Mon, 24 Jul 2023 18:27:08 -0400
On Mon, Jul 24, 2023 at 6:24 PM Frederick Virchanza Gotham via
Std-Proposals <std-proposals_at_[hidden]> wrote:
>
> You'll occasionally see the following inside C++ files:

"Some people write this code" is not, in and of itself, a good
justification for a language change.

>
> MyClass &GetMyClass(void)
> {
> static MyClass obj;
> return obj;
> }
>
> #define g_obj (GetMyClass())
>
> Since C++11, this code uses a "once_flag" behind the scenes to make
> sure that it's threadsafe.
>
> The only problem here with using the preprocessor to define 'g_obj' is
> that we can't control the scope -- we can't put 'g_obj' inside a
> namespace. We also need to make sure that nobody names anything
> 'g_obj' anywhere.

Um, why though? Why is this code worth protecting?

Received on 2023-07-24 22:27:21