News for hackers. Stuff that matters.
Re:C++ Patterns: The Badge by sorenjan on Tuesday June 11, 2019 @07:10PM (#20160348)
Could you make the badge the last argument and give it a default value, so you don't need the initialization at the function call? It looks a bit strange and would need an explanation for why the empty initialization list is there IMO.

I'm not sure if it would work, cppreference has this to say about default arguments:

> The names used in the default arguments are looked up, checked for accessibility, and bound at the point of declaration, but are executed at the point of the function call

https://en.cppreference.com/w/cpp/language/default_arguments

Re:C++ Patterns: The Badge by akling on Tuesday June 11, 2019 @11:20PM (#20161484)
Oh man, I got really excited when I saw this suggestion and got out of bed early to try it out.

Unfortunately it seems like default arguments can't use private constructors. Both GCC and Clang immediately fail at the declaration. Not fair, C++!

/home/andreas/src/serenity/Kernel/FileSystem/VirtualFileSystem.h:81:52: error: 'Badge<T>::Badge() [with T = Device]' is private within this context

Re:C++ Patterns: The Badge by ryanianian on Tuesday June 11, 2019 @07:59PM (#20160614)
It's an empty struct so the compiler will elide any storage either way, but agreed having it be the first arg is a bit odd. Perhaps it's for regularity in the cases where you want to make variadic functions 'badged' in this way.
Re:C++ Patterns: The Badge by dllthomas on Tuesday June 11, 2019 @11:44PM (#20161609)
Empty structs have size 1, per the C++ spec. The compiler probably won't elide the storage unless it can show that no one probably cares.
Re:C++ Patterns: The Badge by vnorilo on Wednesday June 12, 2019 @01:44AM (#20162067)
In this case, it should absolutely do it.

Take a look: https://godbolt.org/z/DmeYL-

Re:C++ Patterns: The Badge by dllthomas on Wednesday June 12, 2019 @11:03AM (#20165330)
I'm surprised, particularly about the cross-linking bit as that seems to violate the spec (per my recollection of the spec, which is the most likely thing to be wrong).

It's worth noting that it does apply to the latest gcc and Clang but not to all the compilers listed there. For an example of a difference, the latest djggp (7.2.0) inserts an extra push in the call when the empty argument is present.

Re:C++ Patterns: The Badge by vnorilo on Wednesday June 12, 2019 @11:09AM (#20165382)
If you use a calling convention that mandates the use of stack, you would see that push, or at least have a byte reserved.

AFAICT the C++ standard itself does not specify the particulars of calling convention, which is a function of the platform, compiler and flags/attributes.

Re:C++ Patterns: The Badge by ncmncm on Tuesday June 11, 2019 @08:21PM (#20160719)
Yes, a default argument would be cleaner.

There are an astonishing number of uses for default arguments that never appear at the call site.

My fav constructs an object that is just storage for a value that needs to live a little bit longer than the call itself.

Re:C++ Patterns: The Badge by antoniolin on Tuesday June 11, 2019 @11:13PM (#20161442)
Unfortunately, it doesn't work. The reason is that VFS cannot access private constructor of Badge<Device>.