News for hackers. Stuff that matters.
Re:C++ Patterns: The Badge by xleviator on Tuesday June 11, 2019 @11:43PM (#20161608)
In larger codebases with many developers, attention to the API is required in order to be able to maintain code without breaking clients.

The main downside of Badge<T> is that it comes with a performance penalty and ABI change for something that should be statically deducible (empty struct is one byte in C++).

Re:C++ Patterns: The Badge by vnorilo on Wednesday June 12, 2019 @01:32AM (#20162019)
There is no performance penalty in any modern compiler that I am aware of (effectively no code will be emitted).

You can have a look here: https://godbolt.org/z/DmeYL-

Re:C++ Patterns: The Badge by xleviator on Wednesday June 12, 2019 @03:27AM (#20162492)
Sure within a single compilation unit. Otherwise, I like dearrifling's suggestion.
Re:C++ Patterns: The Badge by vnorilo on Wednesday June 12, 2019 @03:40AM (#20162543)
The example also shows a call to a different translation unit. There's no code for passing the empty struct. There's also none for handling it, as you can see from the assembly output.

The one byte rule does nothing here, because its value is undefined and its address not observable.

edit: if you find a case where an empty struct parameter causes code to be emitted, let's consider filing bugs.

Re:C++ Patterns: The Badge by xleviator on Wednesday June 12, 2019 @10:45AM (#20165163)
Oops, I did miss that, but this seems to be compiler or calling convention specific. x86_64 gcc 9.1, linked, shows no difference. However arm64 gcc 8.2 and x86-64 clang show a difference.
Re:C++ Patterns: The Badge by vnorilo on Wednesday June 12, 2019 @11:12AM (#20165406)
thanks for clarification! you’re of course right about the calling convention.
Re:C++ Patterns: The Badge by maxxxxx on Wednesday June 12, 2019 @01:05AM (#20161916)
Don't you have to have pretty high performance needs for this class to make a performance difference? Unless it's called multiple thousand times per second I don't see it making much of a difference.
Re:C++ Patterns: The Badge by dearrifling on Wednesday June 12, 2019 @12:47AM (#20161849)
You can make register_device() an inline function that just calls a do_register_device() private function without the badge. That way you don't have to pay for the empty class.
Re:C++ Patterns: The Badge by jcl on Wednesday June 12, 2019 @01:31AM (#20162016)
I'm not sure I follow... The motivation for Badge is to have methods on VFS that only Device can call, without exposing VFS's internals to Device. Without Badge, what prevents non-Device functions from calling register_device()?
Re:C++ Patterns: The Badge by taylorconor on Wednesday June 12, 2019 @01:37AM (#20162038)
I guess the inline function would require a Badge and the private member function would not. So an inline constructed but never used Badge is even more likely to be completely optimized away
Re:C++ Patterns: The Badge by dearrifling on Wednesday June 12, 2019 @01:41AM (#20162049)