To my chagrin I got caught out by this trick question in an interview:
int func(int &&a)
{
return a * 2;
}
:
int b = func(123);
:
Where the question was "what would happen". I assumed it wouldn't compile being a reference to a literal but it does. So I'm just wondering why on earth and when would you ever do the above? Is there some wierd use case?
boltar@caprica.universe˙writes:
To my chagrin I got caught out by this trick question in an interview:
int func(int &&a)
{
˙˙˙˙˙˙˙ return a * 2;
}
le:
˙˙˙˙˙˙˙ int b = func(123);
:
Where the question was "what would happen". I assumed it wouldn't compi
nbeing a reference to a literal but it does. So I'm just wondering why o
e?earth and when would you ever do the above? Is there some wierd use cas
The only use case I can see is tricky interview questions.˙ ;)
boltar@caprica.universe writes:
To my chagrin I got caught out by this trick question in an interview:
int func(int &&a)
{
return a * 2;
}
:
int b = func(123);
:
Where the question was "what would happen". I assumed it wouldn't compile
being a reference to a literal but it does. So I'm just wondering why on
earth and when would you ever do the above? Is there some wierd use case?
The only use case I can see is tricky interview questions. ;)
boltar@caprica.universe writes:
To my chagrin I got caught out by this trick question in an interview:
int func(int &&a)
{
return a * 2;
}
:
int b = func(123);
:
Where the question was "what would happen". I assumed it wouldn't compile
being a reference to a literal but it does. So I'm just wondering why on
earth and when would you ever do the above? Is there some wierd use case?
The only use case I can see is tricky interview questions. ;)
Where the question was "what would happen". I assumed it wouldn't compile being a reference to a literal but it does. So I'm just wondering why on earth and when would you ever do the above? ...It's there just for completeness.
<snip> Luckily I
wasn't fooled by the other questions though it does make me wonder if I'd want
to work in a place that has this kind of moronic interview process.
Am 14.04.2026 um 16:57 schrieb boltar@caprica.universe:
Where the question was "what would happen". I assumed it wouldn't compileIt's there just for completeness.
being a reference to a literal but it does. So I'm just wondering why on
earth and when would you ever do the above? ...
Jonathan Lamothe <jonathan@jlamothe.net> writes:
boltar@caprica.universe writes:
To my chagrin I got caught out by this trick question in an interview:The only use case I can see is tricky interview questions. ;)
int func(int &&a)
{
return a * 2;
}
:
int b = func(123);
:
Where the question was "what would happen". I assumed it wouldn't compile >>> being a reference to a literal but it does. So I'm just wondering why on >>> earth and when would you ever do the above? Is there some wierd use case? >>
More seriously though, I'm not sure off-hand, but this feels like a case
of undefined behaviour. At any rate, it's terrible for the clarity of
the code, which I try to prioritize.
I hate it when job interview questions don't actually reflect the kind
of work you'll be expected to do. Though, if this *is* the
expectation... bullet dodged, I guess?
But what kind of completeness? You can't "move" or nullify a literal,
its in the read-only text area of the program so whats the point? Surely
it should be a compile time error or at least a warning?
More seriously though, I'm not sure off-hand, but this feels like a case
of undefined behaviour. At any rate, it's terrible for the clarity of
the code, which I try to prioritize.
I hate it when job interview questions don't actually reflect the kind
of work you'll be expected to do. Though, if this *is* the
expectation... bullet dodged, I guess?
I hate it when job interview questions don't actually reflect the kind
of work you'll be expected to do. Though, if this *is* the
expectation... bullet dodged, I guess?
This isn't an interview question because it's not practical.
On Tue, 14 Apr 2026 11:01:22 -0400
Jonathan Lamothe <jonathan@jlamothe.net> gabbled:
boltar@caprica.universe writes:
To my chagrin I got caught out by this trick question in an interview:The only use case I can see is tricky interview questions. ;)
int func(int &&a)
{
return a * 2;
}
:
int b = func(123);
:
Where the question was "what would happen". I assumed it wouldn't compile >>> being a reference to a literal but it does. So I'm just wondering why on >>> earth and when would you ever do the above? Is there some wierd use case? >>
Unfortunately it was that kind of interview where they tried to catch you
out rather than testing your code writing and debugging abilities. Luckily I >wasn't fooled by the other questions though it does make me wonder if I'd want
to work in a place that has this kind of moronic interview process.
boltar@caprica.universe writes:
On Tue, 14 Apr 2026 11:01:22 -0400
Jonathan Lamothe <jonathan@jlamothe.net> gabbled:
boltar@caprica.universe writes:
To my chagrin I got caught out by this trick question in an interview: >>>>The only use case I can see is tricky interview questions. ;)
int func(int &&a)
{
return a * 2;
}
:
int b = func(123);
:
Where the question was "what would happen". I assumed it wouldn't compile >>>> being a reference to a literal but it does. So I'm just wondering why on >>>> earth and when would you ever do the above? Is there some wierd use case? >>>
Unfortunately it was that kind of interview where they tried to catch you >>out rather than testing your code writing and debugging abilities. Luckily I >>wasn't fooled by the other questions though it does make me wonder if I'd want
to work in a place that has this kind of moronic interview process.
I dunno, perhaps they're doing it intentionally to weed out
candidates that did not object to the the silly test?
Am 14.04.2026 um 17:10 schrieb Jonathan Lamothe:
More seriously though, I'm not sure off-hand, but this feels like a case
of undefined behaviour. At any rate, it's terrible for the clarity of
the code, which I try to prioritize.
That's not UB. The code falls back to copying, just like with a class
whose objects aren't move()-able.
I hate it when job interview questions don't actually reflect the kind
of work you'll be expected to do. Though, if this *is* the
expectation... bullet dodged, I guess?
This isn't an interview question because it's not practical.
scott@slp53.sl.home (Scott Lurndal) writes:
I dunno, perhaps they're doing it intentionally to weed out
candidates that did not object to the the silly test?
<snip>
That's one of the few reasons I would accept as valid. I have my doubts >however as to whether this is what they ware actually doing. In my >experience, most employers don't like it when their employees push back
on nonsense.
On Tue, 14 Apr 2026 18:25:09 +0200
Bonita Montero <Bonita.Montero@gmail.com> gabbled:
Am 14.04.2026 um 17:10 schrieb Jonathan Lamothe:
More seriously though, I'm not sure off-hand, but this feels like a case >>> of undefined behaviour.˙ At any rate, it's terrible for the clarity of
the code, which I try to prioritize.
That's not UB. The code falls back to copying, just like with a class
whose objects aren't move()-able.
It begs the question that if move (&&) is permitted why does it error
with a
standard reference (&)?
It begs the question that if move (&&) is permitted why does it error
with a standard reference (&)?
Am 15.04.2026 um 10:18 schrieb boltar@caprica.universe:
It begs the question that if move (&&) is permitted why does it error
with a standard reference (&)?
Because references on temporaries don't make sense.
On Wed, 15 Apr 2026 14:24:23 +0200
Bonita Montero <Bonita.Montero@gmail.com> gabbled:
Am 15.04.2026 um 10:18 schrieb boltar@caprica.universe:
It begs the question that if move (&&) is permitted why does it error
with a standard reference (&)?
Because references on temporaries don't make sense.
Neither does doing a move operation on a integer literal but here we are.
Neither does doing a move operation on a integer literal but here we are.
Am 15.04.2026 um 10:18 schrieb boltar@caprica.universe:
It begs the question that if move (&&) is permitted why does it error
with a standard reference (&)?
Because references on temporaries don't make sense.
On 4/15/2026 5:24 AM, Bonita Montero wrote:
Because references on temporaries don't make sense.
Tell that to some DirectX examples? ;^)
Am 15.04.2026 um 17:07 schrieb boltar@caprica.universe:
Neither does doing a move operation on a integer literal but here we are.
If you want to know everything about move semantics try that:
https://leanpub.com/cppmove
262 pages about such a simple feature ...; C++ is crazy.
Am 16.04.2026 um 08:08 schrieb Chris M. Thomasson:
On 4/15/2026 5:24 AM, Bonita Montero wrote:
Because references on temporaries don't make sense.
Tell that to some DirectX examples? ;^)
It's simply not possible:
˙˙˙˙int main()
˙˙˙˙{
˙˙˙˙˙˙˙ []( int &i )
˙˙˙˙˙˙˙ {
˙˙˙˙˙˙˙˙˙˙˙ return i + 1;
˙˙˙˙˙˙˙ }( 456 );
˙˙˙˙}
Gives a compile-time error.
This works:
˙˙˙˙int main()
˙˙˙˙{
˙˙˙˙˙˙˙ []( const int &i ) -> int
˙˙˙˙˙˙˙ {
˙˙˙˙˙˙˙˙˙˙˙ return i + 1;
˙˙˙˙˙˙˙ }( 456 );
˙˙˙˙˙˙˙ []( int &&i ) -> int
˙˙˙˙˙˙˙ {
˙˙˙˙˙˙˙˙˙˙˙ return i + 1;
˙˙˙˙˙˙˙ }( 456 );
˙˙˙˙}
On Wed, 15 Apr 2026 17:51:57 +0200
Bonita Montero <Bonita.Montero@gmail.com> gabbled:
Am 15.04.2026 um 17:07 schrieb boltar@caprica.universe:
Neither does doing a move operation on a integer literal but here we are. >>If you want to know everything about move semantics try that:
https://leanpub.com/cppmove
262 pages about such a simple feature ...; C++ is crazy.
Yes, the language is ridiculously overspecced now. I think I've said on this >group before that if I was starting out in dev I'd take one look at C++ and >flee to Python. Back when I learnt it in the early 2000s a person could
learn pretty much the entire language including the STL. There's zero chance >anyone could now.
boltar@caprica.universe writes:
On Wed, 15 Apr 2026 17:51:57 +0200
Bonita Montero <Bonita.Montero@gmail.com> gabbled:
Am 15.04.2026 um 17:07 schrieb boltar@caprica.universe:
Neither does doing a move operation on a integer literal but here we are. >>>If you want to know everything about move semantics try that:
https://leanpub.com/cppmove
262 pages about such a simple feature ...; C++ is crazy.
Yes, the language is ridiculously overspecced now. I think I've said on this >>group before that if I was starting out in dev I'd take one look at C++ and >>flee to Python. Back when I learnt it in the early 2000s a person could >>learn pretty much the entire language including the STL. There's zero chance >>anyone could now.
When I learned C++ in 1989, STL didn't exist and the C++ syntax was translated to C (cfront).
That was the true "C with classes".
To my chagrin I got caught out by this trick question in an interview:
int func(int &&a)
{
return a * 2;
}
:
int b = func(123);
:
Where the question was "what would happen". I assumed it wouldn't compile being a reference to a literal but it does. So I'm just wondering why on earth and when would you ever do the above? Is there some wierd use case?
Moreover, there's no
"move semantics" in C++ language. "Move semantics" is a user-level
concept - something you implement yourself in your user-level code. The
only thing C++ the language does is provide some low-level primitives to help you do that (e.g. rvalue references). But the above example has no "move semantics".
On Wed, 15 Apr 2026 14:24:23 +0200
Bonita Montero <Bonita.Montero@gmail.com> gabbled:
Am 15.04.2026 um 10:18 schrieb boltar@caprica.universe:
It begs the question that if move (&&) is permitted why does it error
with a standard reference (&)?
Because references on temporaries don't make sense.
Neither does doing a move operation on a integer literal but here we are.
Luckily I
wasn't fooled by the other questions though it does make me wonder if I'd want
to work in a place that has this kind of moronic interview process.
template-related chaff. They just intended to check whether you are at
home with the very basics, e.g. whether you understand that
int &&r = 123;
is already perfectly valid. But your concerns about "moving a literal"
(?!) already indicate that such basics are still well outside your reach.
On Sat, 18 Apr 2026 10:16:21 -0700
Andrey Tarasevich <noone@noone.net> gabbled:
template-related chaff. They just intended to check whether you are at >>home with the very basics, e.g. whether you understand that
int &&r = 123;
is already perfectly valid. But your concerns about "moving a literal"
Yes , a literal, a value stored in the *read-only* program text area:
int main()
{
int &&i = 123;
}
gives
movl $123, -12(%rbp)
See that $5? Its hard coded.
On Sat, 18 Apr 2026 10:16:21 -0700
Andrey Tarasevich <noone@noone.net> gabbled:
template-related chaff. They just intended to check whether you are at
home with the very basics, e.g. whether you understand that
˙ int &&r = 123;
is already perfectly valid. But your concerns about "moving a literal"
Yes , a literal, a value stored in the *read-only* program text area:
int main()
{
˙˙˙˙int &&i = 123;
}
gives
˙˙˙˙˙˙ movl˙˙˙ $123, -12(%rbp)
| Sysop: | Tetrazocine |
|---|---|
| Location: | Melbourne, VIC, Australia |
| Users: | 15 |
| Nodes: | 8 (0 / 8) |
| Uptime: | 180:25:39 |
| Calls: | 213 |
| Files: | 21,502 |
| Messages: | 84,274 |