Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Strange behaviour ...

13 views
Skip to first unread message

Bonita Montero

unread,
Nov 22, 2021, 10:47:21 AM11/22/21
to
Look at this code:

#include <iostream>
#include <vector>

using namespace std;

int main( int argc, char *argv )
{
vector<int> vi1, vi2;
for( int i = 0; i != 10; ++i )
vi1.emplace_back( i ),
vi2.emplace_back( i );
vector<int> viR = argc < 2 ? vi1 : move( vi2 );
cout << vi1.size() << endl;
cout << vi2.size() << endl;
cout << viR.size() << endl;
}

If you specify no commandline-parameter viR gets a copy of vi1.
If you specify a commandline-parameter, viR gets the moved con-
tents of vi2. Is this specified behaviour that the result of this
expression is dependent on the path, i.e. it's an lvalue for vi1
and rvalue for vi2 ? What I'm wonderning about is: the result of
the ternary operator should have the same type for all paths. It
would have been more reasonable for me if the compiler would give
an error because of incompatible types.

Bonita Montero

unread,
Nov 22, 2021, 10:51:32 AM11/22/21
to
I've got it. I just made:

(argc < 2 ? vi1 : move( vi2 )).emplace_back( 100 );

And if argc >= 2, vi2 got empty afterwards. So vi2 is moved into
a temporary-obect which has the same lvalue-charactistics like vi1.
0 new messages