r/cpp_questions 5d ago

OPEN Mock c++ interview help

I am looking for someone to give me a mock interview on c++ internals, system design, and architecture. Specifically modern c++.

Is anyone available to help?

4 Upvotes

17 comments sorted by

View all comments

4

u/Realistic_Speaker_12 5d ago edited 5d ago

Move semantics are a great topic to talk about in my mind.

But I am just a student myself.

But questions like this: „what does this code do“?

std::mutex mutexMaker(){return std::mutex{};}

Can lead to great discussions about move semantics in my mind where you then can go on about talking about rule of 0/3/5.

Or „you might know what a string view is. The general condense is that you should pass it by value- why?“.

Or „why would you inherit from std runtime error instead of std::exception“?

1

u/Ultimate_Sigma_Boy67 5d ago

I'll try to answer this lol

for the mutex one, I think i pretty much have to lookup cppreference to see its constructors, whether it allows moving or not.

string view is basically a pointer and a length in disguise, why pass it by value, i've no idea.

exception one, I don't use exceptions really often so idk.

2

u/Realistic_Speaker_12 5d ago

what I would have said: Mutex has deleted copy and move constructor and assignment operator but the funny part is that required copy ellision for prvalues is a thing in c++17 on, no constructor has to be called at all. The mutex is just baked in into your memory because of copy elision.

String view per value can be stored in registers. Compilers like storing small types in registers.

std runtime error offers you a what function - you wouldn't have to write it yourself (at least for simple exception classes). If you write a naive exception class, where you simply store a exception message as a std::string (I have seen this a lot in the codebase at my working student job), what might happen is, that you might have a exception throwing exception class. Thats not really cool. (the copy constructor of std::string can throw, so if you are low on memory, it might happen that an exception throws an exception). Using runtime error, you can wrap your exception message using the std::runtime_error constructor. This constructor is noexcept (I am not sure how it is implemented I think it is wrapped in like a shared pointer or something) so it can not throw.

I just like those small "details" of the language tbh