Prev: Templates rejected on non-dependent parameters before deduction in C++0X?
Next: Templates rejected on non-dependent parameters before deduction in C++0X?
From: mzdude on 11 Jun 2010 07:35 On Jun 10, 9:22 pm, PolarPeter <polarpeterb...(a)gmail.com> wrote: > One of many questions I typically ask candidates who come to interview > with me is this one: > > class A > { > public: > A() : y(2), x(y * 2) {} > ~A() {} > > private: > int x; > int y; > > }; > > int main() > { > A a; > return 0; > > } > > I ask them what the value of a.x and a.y will be after 'a' has been > constructed. > > Clearly, the issue here is that x is constructed first, and uses the > uninitialized value of y. However, a lot of candidates miss the order > issue, and think that a.y is 2, and a.x is 4. > > I think this question is pretty fundamental, given how often MILs are > used. I think people who say they have 10+ years professional > experience should be able either to see the issue, or at least talk > through why it works the way it works if they don't initially see it > but are then shown. > > I'd like to know what you think - is it reasonable to expect > candidates who claim they have 10 years+ 'advanced' C++ experience to > know this? > I think it's a fair and reasonable question. For those who don't get the correct answer a follow up question might be why is x == 0 and y == 2. At our shop we run PC-Lint and it does catch the out of order MIL. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Bo Persson on 11 Jun 2010 07:34 PolarPeter wrote: > One of many questions I typically ask candidates who come to > interview with me is this one: > > class A > { > public: > A() : y(2), x(y * 2) {} > ~A() {} > > private: > int x; > int y; > }; > > int main() > { > A a; > return 0; > } > > I ask them what the value of a.x and a.y will be after 'a' has been > constructed. > > Clearly, the issue here is that x is constructed first, and uses the > uninitialized value of y. However, a lot of candidates miss the > order issue, and think that a.y is 2, and a.x is 4. > > I think this question is pretty fundamental, given how often MILs > are used. I think people who say they have 10+ years professional > experience should be able either to see the issue, or at least talk > through why it works the way it works if they don't initially see it > but are then shown. > > I'd like to know what you think - is it reasonable to expect > candidates who claim they have 10 years+ 'advanced' C++ experience > to know this? > It is reasonable for candidates to know this, but perhaps not to notice the problem at a glance. I did not. .-) One problem with this kind of interview questions is "What was the question?". My first line of thought was "What is y, and why is it set to 2? What does 2 mean? Magic constants, and everything...". Bo Persson -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Daniel Krügler on 11 Jun 2010 07:33 On 11 Jun., 13:34, "Martin B." <0xCDCDC...(a)gmx.at> wrote: > PolarPeter wrote: > > One of many questions I typically ask candidates who come to interview > > with me is this one: > > > class A > > { > > public: > > A() : y(2), x(y * 2) {} > > ~A() {} > > > private: > > int x; > > int y; > > }; > > > int main() > > { > > A a; > > return 0; > > } > > > I ask them what the value of a.x and a.y will be after 'a' has been > > constructed. > > > Clearly, the issue here is that x is constructed first, and uses the > > uninitialized value of y. However, a lot of candidates miss the order > > issue, and think that a.y is 2, and a.x is 4. > > Under MSVC 2005 this give the result of y=2 and x=4. (With Language > extensions disabled and with /W4 which doesn't give any warnings.) > > The constructing order of the specific constructor is relevant, and > *not* the declaration order of the members. (Or so I thought.) You are misinformed. It is very important that the rules are as they are specified, because otherwise safe construction-destruction cycles won't be possible in C++. This is so, because in case of an exception during the construction of an object of class-type, the destructors of all completely constructed sub-objects will invoked. This is quite easy to realize, if the construction order for all sub-elements is identical, because the destructor does only need to start with the last successfully constructed one to undo this. Since the behaviour of the shown program is already undefined (because it performs a read access to an uninitialized variable), the compiler may produce anything - among those something a programmer may have expected. HTH & Greetings from Bremen Daniel Kr�gler -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Goran on 11 Jun 2010 07:35 On Jun 11, 3:22 am, PolarPeter <polarpeterb...(a)gmail.com> wrote: > One of many questions I typically ask candidates who come to interview > with me is this one: > > class A > { > public: > A() : y(2), x(y * 2) {} > ~A() {} > > private: > int x; > int y; > > }; > > int main() > { > A a; > return 0; > > } > > I ask them what the value of a.x and a.y will be after 'a' has been > constructed. > > Clearly, the issue here is that x is constructed first, and uses the > uninitialized value of y. However, a lot of candidates miss the order > issue, and think that a.y is 2, and a.x is 4. > > I think this question is pretty fundamental, given how often MILs are > used. I think people who say they have 10+ years professional > experience should be able either to see the issue, or at least talk > through why it works the way it works if they don't initially see it > but are then shown. > > I'd like to know what you think - is it reasonable to expect > candidates who claim they have 10 years+ 'advanced' C++ experience to > know this? I do know about this, but frankly, I am not happy about knowing that. The example is __massively__ contrived because real-world code should not be using member construction order anyhow. Imagine that, for documentation purposes or refactoring, you invert that order and suddenly, there's a bug. So such things have no place in real world. But of course, the biggest problem of all is that this code relies on values that come from an object that has not been initialized. There has to be a big fat explanation on why would anyone want to play with that fire. Don't you have more pertinent questions to ask? ;-) Goran. -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Chris Morley on 11 Jun 2010 07:42
>> class A >> { >> public: >> A() : y(2), x(y * 2) {} >> ~A() {} >> >> private: >> int x; >> int y; >> }; >> >> int main() >> { >> A a; >> return 0; >> } >> Clearly, the issue here is that x is constructed first, and uses the >> uninitialized value of y. However, a lot of candidates miss the order >> issue, and think that a.y is 2, and a.x is 4. > > Under MSVC 2005 this give the result of y=2 and x=4. (With Language > extensions disabled and with /W4 which doesn't give any warnings.) > > The constructing order of the specific constructor is relevant, and > *not* the declaration order of the members. (Or so I thought.) Really? VS2008 doesn't warn on /w4 and behaves as expected... i.e. not x=4,y=2 Swapping the int x & int y will gen 4,2 so declaration order is key as is it is supposed to be. Perhaps MS compilers are becoming more standards compliant after all ;) Chris -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |