Conditional ? : operator with class constructor
up vote
11
down vote
favorite
could someone explain me why c
and c1
are constructed different way.
I understand that I have reference to copy created by '?' operator, which is destroyed after construction, but why in first case it behave other way.
I've tested if its optimization, but even with conditions read from console, I have same result. Thanks in advance
#include <vector>
class foo {
public:
foo(const std::vector<int>& var) :var{ var } {};
const std::vector<int> & var;
};
std::vector<int> f(){
std::vector<int> x{ 1,2,3,4,5 };
return x;
};
int main(){
std::vector<int> x1{ 1,2,3,4,5 ,7 };
std::vector<int> x2{ 1,2,3,4,5 ,6 };
foo c{ true ? x2 : x1 }; //c.var has expected values
foo c1{ true ? x2 : f() }; //c.var empty
foo c2{ false ? x2 : f() }; //c.var empty
foo c3{ x2 }; //c.var has expected values
}
c++ c++14 conditional-operator
add a comment |
up vote
11
down vote
favorite
could someone explain me why c
and c1
are constructed different way.
I understand that I have reference to copy created by '?' operator, which is destroyed after construction, but why in first case it behave other way.
I've tested if its optimization, but even with conditions read from console, I have same result. Thanks in advance
#include <vector>
class foo {
public:
foo(const std::vector<int>& var) :var{ var } {};
const std::vector<int> & var;
};
std::vector<int> f(){
std::vector<int> x{ 1,2,3,4,5 };
return x;
};
int main(){
std::vector<int> x1{ 1,2,3,4,5 ,7 };
std::vector<int> x2{ 1,2,3,4,5 ,6 };
foo c{ true ? x2 : x1 }; //c.var has expected values
foo c1{ true ? x2 : f() }; //c.var empty
foo c2{ false ? x2 : f() }; //c.var empty
foo c3{ x2 }; //c.var has expected values
}
c++ c++14 conditional-operator
add a comment |
up vote
11
down vote
favorite
up vote
11
down vote
favorite
could someone explain me why c
and c1
are constructed different way.
I understand that I have reference to copy created by '?' operator, which is destroyed after construction, but why in first case it behave other way.
I've tested if its optimization, but even with conditions read from console, I have same result. Thanks in advance
#include <vector>
class foo {
public:
foo(const std::vector<int>& var) :var{ var } {};
const std::vector<int> & var;
};
std::vector<int> f(){
std::vector<int> x{ 1,2,3,4,5 };
return x;
};
int main(){
std::vector<int> x1{ 1,2,3,4,5 ,7 };
std::vector<int> x2{ 1,2,3,4,5 ,6 };
foo c{ true ? x2 : x1 }; //c.var has expected values
foo c1{ true ? x2 : f() }; //c.var empty
foo c2{ false ? x2 : f() }; //c.var empty
foo c3{ x2 }; //c.var has expected values
}
c++ c++14 conditional-operator
could someone explain me why c
and c1
are constructed different way.
I understand that I have reference to copy created by '?' operator, which is destroyed after construction, but why in first case it behave other way.
I've tested if its optimization, but even with conditions read from console, I have same result. Thanks in advance
#include <vector>
class foo {
public:
foo(const std::vector<int>& var) :var{ var } {};
const std::vector<int> & var;
};
std::vector<int> f(){
std::vector<int> x{ 1,2,3,4,5 };
return x;
};
int main(){
std::vector<int> x1{ 1,2,3,4,5 ,7 };
std::vector<int> x2{ 1,2,3,4,5 ,6 };
foo c{ true ? x2 : x1 }; //c.var has expected values
foo c1{ true ? x2 : f() }; //c.var empty
foo c2{ false ? x2 : f() }; //c.var empty
foo c3{ x2 }; //c.var has expected values
}
c++ c++14 conditional-operator
c++ c++14 conditional-operator
edited Nov 23 at 14:55
Deduplicator
33.9k64787
33.9k64787
asked Nov 23 at 14:30
geniculata
1618
1618
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
20
down vote
The type of a conditional expression is the common type of the two branches, and its value category also depends on them.
For
true ? x2 : x1
, the common type isstd::vector<int>
and the value category is lvalue. This can be tested with:
static_assert(std::is_same_v<decltype((true ? x2 : x1)), std::vector<int>&>);
For
true ? x2 : f()
, the common type isstd::vector<int>
, and the value category is prvalue. This can be tested with:
static_assert(std::is_same_v<decltype((true ? x2 : f())), std::vector<int>>);
Therefore you are storing a dangling reference in c1
. Any access to c1.var
is undefined behavior.
live example on godbolt.org
4
...resulting in UB, if it is accessed after the declaration. That crucial part was omitted from OP's example though.
– Deduplicator
Nov 23 at 14:57
5
It'd be good to explain why these are the common types (ie: because the second case involves an lvalue and prvalue, while the first case is two lvalues).
– Nicol Bolas
Nov 23 at 14:59
Oh C++. Oh you.
– Lightness Races in Orbit
Nov 23 at 15:23
Don't conflate an expression's type with its value category. A?:
never has reference type. Not even the adjusted-away-prior-to-any-further-analysis one.
– T.C.
12 hours ago
@T.C.: updated the answer, please let me know if it's correct now.
– Vittorio Romeo
10 hours ago
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
20
down vote
The type of a conditional expression is the common type of the two branches, and its value category also depends on them.
For
true ? x2 : x1
, the common type isstd::vector<int>
and the value category is lvalue. This can be tested with:
static_assert(std::is_same_v<decltype((true ? x2 : x1)), std::vector<int>&>);
For
true ? x2 : f()
, the common type isstd::vector<int>
, and the value category is prvalue. This can be tested with:
static_assert(std::is_same_v<decltype((true ? x2 : f())), std::vector<int>>);
Therefore you are storing a dangling reference in c1
. Any access to c1.var
is undefined behavior.
live example on godbolt.org
4
...resulting in UB, if it is accessed after the declaration. That crucial part was omitted from OP's example though.
– Deduplicator
Nov 23 at 14:57
5
It'd be good to explain why these are the common types (ie: because the second case involves an lvalue and prvalue, while the first case is two lvalues).
– Nicol Bolas
Nov 23 at 14:59
Oh C++. Oh you.
– Lightness Races in Orbit
Nov 23 at 15:23
Don't conflate an expression's type with its value category. A?:
never has reference type. Not even the adjusted-away-prior-to-any-further-analysis one.
– T.C.
12 hours ago
@T.C.: updated the answer, please let me know if it's correct now.
– Vittorio Romeo
10 hours ago
add a comment |
up vote
20
down vote
The type of a conditional expression is the common type of the two branches, and its value category also depends on them.
For
true ? x2 : x1
, the common type isstd::vector<int>
and the value category is lvalue. This can be tested with:
static_assert(std::is_same_v<decltype((true ? x2 : x1)), std::vector<int>&>);
For
true ? x2 : f()
, the common type isstd::vector<int>
, and the value category is prvalue. This can be tested with:
static_assert(std::is_same_v<decltype((true ? x2 : f())), std::vector<int>>);
Therefore you are storing a dangling reference in c1
. Any access to c1.var
is undefined behavior.
live example on godbolt.org
4
...resulting in UB, if it is accessed after the declaration. That crucial part was omitted from OP's example though.
– Deduplicator
Nov 23 at 14:57
5
It'd be good to explain why these are the common types (ie: because the second case involves an lvalue and prvalue, while the first case is two lvalues).
– Nicol Bolas
Nov 23 at 14:59
Oh C++. Oh you.
– Lightness Races in Orbit
Nov 23 at 15:23
Don't conflate an expression's type with its value category. A?:
never has reference type. Not even the adjusted-away-prior-to-any-further-analysis one.
– T.C.
12 hours ago
@T.C.: updated the answer, please let me know if it's correct now.
– Vittorio Romeo
10 hours ago
add a comment |
up vote
20
down vote
up vote
20
down vote
The type of a conditional expression is the common type of the two branches, and its value category also depends on them.
For
true ? x2 : x1
, the common type isstd::vector<int>
and the value category is lvalue. This can be tested with:
static_assert(std::is_same_v<decltype((true ? x2 : x1)), std::vector<int>&>);
For
true ? x2 : f()
, the common type isstd::vector<int>
, and the value category is prvalue. This can be tested with:
static_assert(std::is_same_v<decltype((true ? x2 : f())), std::vector<int>>);
Therefore you are storing a dangling reference in c1
. Any access to c1.var
is undefined behavior.
live example on godbolt.org
The type of a conditional expression is the common type of the two branches, and its value category also depends on them.
For
true ? x2 : x1
, the common type isstd::vector<int>
and the value category is lvalue. This can be tested with:
static_assert(std::is_same_v<decltype((true ? x2 : x1)), std::vector<int>&>);
For
true ? x2 : f()
, the common type isstd::vector<int>
, and the value category is prvalue. This can be tested with:
static_assert(std::is_same_v<decltype((true ? x2 : f())), std::vector<int>>);
Therefore you are storing a dangling reference in c1
. Any access to c1.var
is undefined behavior.
live example on godbolt.org
edited 10 hours ago
answered Nov 23 at 14:41
Vittorio Romeo
55.8k17149289
55.8k17149289
4
...resulting in UB, if it is accessed after the declaration. That crucial part was omitted from OP's example though.
– Deduplicator
Nov 23 at 14:57
5
It'd be good to explain why these are the common types (ie: because the second case involves an lvalue and prvalue, while the first case is two lvalues).
– Nicol Bolas
Nov 23 at 14:59
Oh C++. Oh you.
– Lightness Races in Orbit
Nov 23 at 15:23
Don't conflate an expression's type with its value category. A?:
never has reference type. Not even the adjusted-away-prior-to-any-further-analysis one.
– T.C.
12 hours ago
@T.C.: updated the answer, please let me know if it's correct now.
– Vittorio Romeo
10 hours ago
add a comment |
4
...resulting in UB, if it is accessed after the declaration. That crucial part was omitted from OP's example though.
– Deduplicator
Nov 23 at 14:57
5
It'd be good to explain why these are the common types (ie: because the second case involves an lvalue and prvalue, while the first case is two lvalues).
– Nicol Bolas
Nov 23 at 14:59
Oh C++. Oh you.
– Lightness Races in Orbit
Nov 23 at 15:23
Don't conflate an expression's type with its value category. A?:
never has reference type. Not even the adjusted-away-prior-to-any-further-analysis one.
– T.C.
12 hours ago
@T.C.: updated the answer, please let me know if it's correct now.
– Vittorio Romeo
10 hours ago
4
4
...resulting in UB, if it is accessed after the declaration. That crucial part was omitted from OP's example though.
– Deduplicator
Nov 23 at 14:57
...resulting in UB, if it is accessed after the declaration. That crucial part was omitted from OP's example though.
– Deduplicator
Nov 23 at 14:57
5
5
It'd be good to explain why these are the common types (ie: because the second case involves an lvalue and prvalue, while the first case is two lvalues).
– Nicol Bolas
Nov 23 at 14:59
It'd be good to explain why these are the common types (ie: because the second case involves an lvalue and prvalue, while the first case is two lvalues).
– Nicol Bolas
Nov 23 at 14:59
Oh C++. Oh you.
– Lightness Races in Orbit
Nov 23 at 15:23
Oh C++. Oh you.
– Lightness Races in Orbit
Nov 23 at 15:23
Don't conflate an expression's type with its value category. A
?:
never has reference type. Not even the adjusted-away-prior-to-any-further-analysis one.– T.C.
12 hours ago
Don't conflate an expression's type with its value category. A
?:
never has reference type. Not even the adjusted-away-prior-to-any-further-analysis one.– T.C.
12 hours ago
@T.C.: updated the answer, please let me know if it's correct now.
– Vittorio Romeo
10 hours ago
@T.C.: updated the answer, please let me know if it's correct now.
– Vittorio Romeo
10 hours ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448536%2fconditional-operator-with-class-constructor%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown