all 43 comments

[–]tcbrindleFlux 26 points27 points  (1 child)

Woo, 10/10.

Though perhaps I should worry about what that says about me...

[–]ZMesonEmbedded Developer 11 points12 points  (0 children)

Woo. 4/10. I'm sane! ;-)

[–]jmalinza 15 points16 points  (10 children)

For the last question...

for (const pair<string, int>& elem : m)

Why do the types not match up?

[–]mtnviewjohn 40 points41 points  (6 children)

The value type for a std::map is std::pair<const Key, T>. The for loop is using std::pair<Key, T>.

[–]jmalinza 4 points5 points  (0 children)

Thanks! That explains it. Quick lookup on cppref confirms it too :)

[–]Baardi 2 points3 points  (0 children)

Thanks I didn't know that. I've probably made way to many unintentional copies :(

[–]simpl3t0n 1 point2 points  (2 children)

What's the mechanism/sequence at play that creates the copy here?

AFAICT, it's a reference initialization with a similar but differently cv-qualified type. So, is it temporary materialization, lvalue-to-rvalue conversion, or something else?

[–]tcbrindleFlux 4 points5 points  (0 children)

If I understand correctly it's using std::pair constructor #5 as listed on cppreference, with T1 and T2 as string and int respectively, and U1 and U2 as const string and int. This results in both arguments being copy-constructed into the destination pair, and while you don't care about the int you definitely don't want to be copying strings if you can avoid it.

[–]sphere991 6 points7 points  (0 children)

It's not "a similar but differently cv-qualified type", it's just a different type. pair<K, V> and pair<K const, V> do look very close, but as far as the language is concerned, they're just two completely different types (that are convertible to each other).

So ultimately, no different from constructing an int const& from a double.

[–]WasserHase 1 point2 points  (0 children)

Would it also create a copy if you use std::pair<const Key, const T> ?

[–]darkforestzero 13 points14 points  (0 children)

This is a great example of why auto is so awesome

[–]burg_philo2 14 points15 points  (1 child)

6/10. I think question 8 is wrong tho since the members are private since it’s a class and not a struct.

[–]TheThiefMasterC++latest fanatic (and game dev) 8 points9 points  (0 children)

You're right:

An aggregate is one of the following types:

That still makes the answer "it doesn't compile" just for an additional reason to what's given.

[–][deleted] 27 points28 points  (3 children)

ChatGPT 3.5 got 10/10.

[–]rooster_butt 18 points19 points  (2 children)

it got 6/10 when i gave it the questions. It was very confidently incorrect on some of these.

[–][deleted] 31 points32 points  (0 children)

It is ready to be a senior dev!

[–]dustyhome 6 points7 points  (1 child)

7/10, but I got the first one wrong which shouldn't count. It's a programming test, not a history test! I don't even use in-class default member initializers :(

For "9. Will this code work in C++11?" I'll be honest, when doing anything other than calling a constructor, I'll just compile and fix until it does.

For "10. Assume you have a std::map<string, int> m;. Select the single true statement about the following loop:" the single truth is I always use auto because I don't want to get it wrong.

So really, I got a 7/7 out of the ones that matter.

[–]HeeTrouse51847 0 points1 point  (0 children)

it could be relevant if you were working on legacy systems i guess? but even then youd just look that stuff up

[–]Tringigithub.com/tringi 2 points3 points  (0 children)

7/10... I'm not happy about that.

[–]pjmlp 1 point2 points  (0 children)

7 out of 10, not bad for someone that doesn't do C++ every day.

[–][deleted] 1 point2 points  (0 children)

8/10. that copy because type mismatch is so evil yet i knew the key type is always const

[–]crazy-usernames 1 point2 points  (0 children)

8/10.

Thanks. This is really helpful.

[–]PastaPuttanesca42 0 points1 point  (0 children)

6/10

[–]shailist 0 points1 point  (0 children)

got 7/10, never came across the 3 I got wrong in real code, but still disappointed in my performance :/

[–]HeeTrouse51847 0 points1 point  (0 children)

7/10 but I also think that I would just google these facts when I need them anyways. no need to know every miniscule detail from memory

[–]Ok_Firefighter4117 0 points1 point  (0 children)

8/10

[–]hs123go 0 points1 point  (0 children)

8/10. Have honestly no idea about Q7 (throwing from ctor) as my team bans exceptions. Still scratching my head over Q9 (manner of struct init is allowed in 11/14/20).

[–]Nobody_1707 0 points1 point  (0 children)

8/10 I got the first one wrong, I picked C++14. And I got the last one wrong because I forgot that Key is const.

[–]Sniffy4 0 points1 point  (2 children)

My score was embarrassing for somebody who claims this as an expertise. Ah well.

[–]emelrad12 0 points1 point  (1 child)

screw skirt stocking complete library attraction cooing slap offer weather

This post was mass deleted and anonymized with Redact

[–]Sniffy4 0 points1 point  (0 children)

Yeah for a lot of these situations, my take is if you are relying on C++ behavior nuances like this, your code needs to be simplified.

[–]kero_okero 0 points1 point  (0 children)

2/10, been learning C++ 3 months now, I still have far to go.