3 ms·
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 wo
by sorenjan 7y ago
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 https://en.cppreference.com/w/cpp/language/default_arguments
- ryanianian 7y agoIt'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.
- dllthomas 7y agoEmpty 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.
- vnorilo 7y agoIn this case, it should absolutely do it. Take a look: https://godbolt.org/z/DmeYL- https://godbolt.org/z/DmeYL-
- dllthomas 7y agoI'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.
- vnorilo 7y agoIf 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.
- ncmncm 7y agoYes, 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.
- antoniolin 7y agoUnfortunately, it doesn't work. The reason is that VFS cannot access private constructor of Badge<Device>.
- akling 7y agoOh 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