Cppquiz – Figure what a given C++ program does
cppquiz.org
cppquiz.org
<now for the but> In my over 10 years as a developer, I rarely if EVER come across these sorts of problems. I've been in many problem domains and modeled many sorts of problems. I have come across these sorts of issues maybe 1 or 2 times. When I did come across this sort of problem a few hours of thought allowed me to come up with a rational solution that didn't require stretching the bounds a of a language. </but>
So while Im all for academic curiosity and figuring out the way things work, I dont know that these sort of things are applicable as to whether someone can write code to do a job that is robust, scalable and reliable.
So please excuse me while I go read my C++ books because I feel stupid...
As an example, in a program I'm working on which is still some ridiculously simple 100-line program, I shadowed a variable name inside a loop. (C++) I figured it would be ok because I was declaring the new variable after I was done using the previous one. But apparently, declaring a variable at all in a scope causes the new variable to be the one used inside that scope, even from the beginning. And considering the two variables were of a different type, it was causing a problem. Not hard to find in a small program, but might be harder to find in a large one.
On the flipside, a lot of these issues might be relatively easy to find with a little intelligent Googling or asking Stack Overflow. But that time might have been better spent continuing development on the current project. And that time can add up.
My story was a C# WPF application and certain events were not being unregistered. It wasn't the language built in event registration but a weak even registration handler in a WPF library.
It wasn't unregistering the event and it was odd to say the least because it was just an equals call. It turns out that the function was an anonymous function (that was defined as x = {...};) The strangest thing is that the anonymous function had a reference to it and I had thought that the references were being compared for equality.
But it wasn't being matched... So I recommended the inline declaration be changed to an actual static method and the comparison worked. I think I gave some kind of explanation about how it most likely compared the address of the methods or something. Or the comparer for a Action<T> was different than "normal" objects. Half the fun of it is figuring out how things really work behind the scenes though. For the person figuring it out though.
http://cppquiz.org/quiz/question/38
The answer is in the C++ name.
C++14's decltype(auto) confuses things even further: http://coliru.stacked-crooked.com/a/f7d8195efc4708f3
The question is what should someone say about his/her level of expertise when asked by a recruiter? I have been comfortably programming in the language for the past 15+ years, still feel shy about claiming myself to be an expert.
[1] http://herbsutter.com/2008/01/01/gotw-88-a-candidate-for-the...
Array(10).join("hn" - 1) + " Batman!"Understanding the first rested on the fact that a friend function declaration requires that the relevant function declaration be present above it in the translation unit (e.g. friend int A::foo();" requires that "class A" be defined, even though the prototype is otherwise correct). Meaningless trivia, and something every compiler will remind you about instantly and clearly.
The second one was pretty straightforward and basically about understanding that a superclass constructor is invoked before the subclass constructor. That's certainly important to understand, but in my experience rarely misunderstood except by true OO newbies (basically the same behavior has to hold for any static type system with inheritance).
The third called a public virtual function through a superclass pointer and expected you to recognize that declaring the derived function private in the subclass that the object belongs to didn't matter. Again, trivia that doesn't correspond to any meaningful real-world code pattern.
I gave up after that. Yawn.
Or the one that makes sure you understand that, if a function isn't declared virtual, then you get static dispatch instead.
Or, heck, the one that ensures you know that floating-point literals default to double, and that this fact affects function overload resolution.
The next logical step is to start defining or disallowing more of the undefined ones.