0

Does the code below call int constructor?

int a(15);

I am asking this question because I failed one test on LinkedIn:

enter image description here

it this test correct?

Dmitriano
  • 1,878
  • 13
  • 29
  • 5
    This is a poorly-worded question. It looks like the answer they're angling for is 2, but that's a reason to declare it as a `const` reference instead of a non-`const` reference when the question is asking about a reason to declare it as `const` reference instead of passing by value. But the other three answers are just outright incorrect. – Nathan Pierson Apr 27 '22 at 16:38
  • If you're saying that it claims #4 is correct, that is a *very* weird answer. A class instance can be passed to a function without reference semantics, but 1) The function would receive a *copy* of the instance (allowing it to change it, but the changes would only apply to their own copy) 2) If the function receives a base class, and the caller passes a child of that base, you end up slicing (not the useful Python version, the horrible C++ version), which does weird/bad things. But case #1 is the same for objects and primitives; if you receive by value, you have your own copy. – ShadowRanger Apr 27 '22 at 16:40
  • 1
    That's a terribly worded question, but I concur, #2 is the only reasonable answer, though it completely ignores the latter half of the question, but is the only answer that isn't otherwise-garbage. It's the political step-back answer: when you don't want to, or know, the answer to a question, just answer a different question, even if it wasn't asked. – WhozCraig Apr 27 '22 at 16:42
  • I agree with @NathanPierson that if it's indicating #4 is *incorrect*, that's fine; the correct answer is clearly #2 (the benefit is that it gets the performance of passing by reference without making copies, without the caller needing to worry if the function will modify what they gave it). – ShadowRanger Apr 27 '22 at 16:42
  • 2
    What does your snippet have to do with LI question? The question shows declaration of a function taking const reference to `my array` (whatever that is) and returning an int. Your code shows integer being created and initialized to 15, but how should those two pieces of code be related? – alagner Apr 27 '22 at 16:43
  • Even the correct answer is #2, you can still remove the `const` via a cast and still modify the contents. It's a compiler/programmer hint. Besides in low level `const` variables occupy different address space. – Ilian Zapryanov Apr 27 '22 at 16:44
  • @IlianZapryanov well, that most likely will be UB, so ”can” is a bit strong wording here imho. But granted, it works oftentimes. – alagner Apr 27 '22 at 16:45
  • 1
    @IlianZapryanov That's only partially true. You can't know if the function was given a `const` object from the call site. If it was, then casting away `const` and mutating the object would be UB. – NathanOliver Apr 27 '22 at 16:45
  • 1
    @IlianZapryanov you can only legally const-cast-and-modify an object out of a const reference if the original was *not* const to begin with; otherwise you invoke UB. – WhozCraig Apr 27 '22 at 16:45
  • @NathanOliver you can use type traits to distinct that: `std::is_const_v` – Ilian Zapryanov Apr 27 '22 at 16:48
  • @IlianZapryanov Adding `const`to a reference protects against *accidentally* modifying the referred object using that reference. Which is what the answer is referring to. It does not protect against self-sabotage, which casting the const away would most likely be. – François Andrieux Apr 27 '22 at 16:48
  • @IlianZapryanov That doesn't work, you can't use type traits to know if the referred object is actually const or not. Type traits are entirely based on statically known type information. If the function argument is const the type trait will report it as const regardless of what it actually refers to. – François Andrieux Apr 27 '22 at 16:49
  • @FrançoisAndrieux I am aware of that, however the test pointed out says that the programmer will be sure nothing can change. – Ilian Zapryanov Apr 27 '22 at 16:50
  • @IlianZapryanov How would you do that inside the function? Could you give me an example. The example I think of that will blow up is [this](http://coliru.stacked-crooked.com/a/28c25a8ecf4dcd4f) – NathanOliver Apr 27 '22 at 16:52
  • @IlianZapryanov The answer is poorly worded. The object could still change during the function call, but that change won't be due to the function using the argument for that change. For example the argument may refer to a global object which the function changes independently of the argument. But `const_cast` is not a good counter example for the poor wording. Just about every general statement about C++ has the implication that hidden code is minimally reasonable. You have to assume the implementer of the function respects const correctness, or `const` becomes entirely meaningless. – François Andrieux Apr 27 '22 at 16:53
  • @NathanOliver not a one line fix, but I may come up with a workaround with template wrapper, but is it necessary? The point is that you can still abuse `const`. – Ilian Zapryanov Apr 27 '22 at 16:57
  • @NathanOliver What stops you using a C cast in your example ? https://pastebin.com/9GE0mp1c – Ilian Zapryanov Apr 27 '22 at 17:02
  • 1
    @IlianZapryanov What's the point to use C cast? the `const_cast` does compile already. And it's UB either way. – apple apple Apr 27 '22 at 17:06
  • @IlianZapryanov The c-cast and `const_cast` do the same thing, ie badness. – NathanOliver Apr 27 '22 at 17:27
  • @NathanOliver well, I didn't pointed out that it's a good thing, right? I am just saying it's doable. – Ilian Zapryanov Apr 27 '22 at 17:29

0 Answers0