In Stroustrup's example, what does this colon mean in `return 1 : 2`? It's not a label or ternary operatorWhat does the explicit keyword mean?What does “static” mean in C?What does map(&:name) mean in Ruby?What does the question mark and the colon (?: ternary operator) mean in objective-c?What does the star operator mean?What does int argc, char *argv[] mean?What is the meaning of prepended double colon “::”?What does “dereferencing” a pointer mean?What does T&& (double ampersand) mean in C++11?C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?
Why do the lights go out when someone enters the dining room on this ship?
Why doesn't Rocket Lab use a solid stage?
How to translate "Keywords" to Spanish "Palabras claves"
correct spelling of "carruffel" (fuzz, hustle, all that jazz)
CPLD based Pierce oscillator
Is Germany still exporting arms to countries involved in Yemen?
German characters on US-International keyboard layout
return tuple of uncopyable objects
Conditional probability - sum of dice is even given that at least one is a five
Jumping frame contents with beamer and pgfplots
Ito`s Lemma problem
Are there any established rules for splitting books into parts, chapters, sections etc?
Why is a set not a partition of itself?
Why did I need to *reboot* to change my group membership
what does a native speaker say when he wanted to leave his work?
How can a layman easily get the consensus view of what academia *thinks* about a subject?
Replace all items that are not belong to characters and numbers by ' '
Can I use my laptop, which says 100-240V, in the USA?
LWC1513: @salesforce/resourceUrl modules only support default imports
Why in the below sentence dative "dem" is used instead of nominative "der"?
How do employ ' ("prime") in math mode at the correct depth?
Unexpected Netflix account registered to my Gmail address - any way it could be a hack attempt?
using `is` operator with value type tuples gives error
Why does my circuit work on a breadboard, but not on a perfboard? I am new to soldering
In Stroustrup's example, what does this colon mean in `return 1 : 2`? It's not a label or ternary operator
What does the explicit keyword mean?What does “static” mean in C?What does map(&:name) mean in Ruby?What does the question mark and the colon (?: ternary operator) mean in objective-c?What does the star operator mean?What does int argc, char *argv[] mean?What is the meaning of prepended double colon “::”?What does “dereferencing” a pointer mean?What does T&& (double ampersand) mean in C++11?C++11 introduced a standardized memory model. What does it mean? And how is it going to affect C++ programming?
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;
I don't understand one particular use of a colon.
I found it in the book "The C++ programming language" by Bjarne Stroustrup, 4th edition, section 11.4.4, page 297:
void g(double y)
[&] f(y); // return type is void
auto z1 = [=](int x) return x+y; // return type is double
auto z2 = [=,y] if (y) return 1; else return 2; // error : body too complicated
// for return type deduction
auto z3 =[y]() return 1 : 2; // (Me: HERE!!!) return type is int
auto z4 = [=,y]()−>int if (y) return 1; else return 2; // OK: explicit return type
All comments from Stroustrup, except the one inside the parentheses.
I have no idea what it could be.
It seems like a conditional ternary operator without the first member (and without the "?"), but in that case I don't understand how it could work without a condition.
c++ syntax
|
show 4 more comments
I don't understand one particular use of a colon.
I found it in the book "The C++ programming language" by Bjarne Stroustrup, 4th edition, section 11.4.4, page 297:
void g(double y)
[&] f(y); // return type is void
auto z1 = [=](int x) return x+y; // return type is double
auto z2 = [=,y] if (y) return 1; else return 2; // error : body too complicated
// for return type deduction
auto z3 =[y]() return 1 : 2; // (Me: HERE!!!) return type is int
auto z4 = [=,y]()−>int if (y) return 1; else return 2; // OK: explicit return type
All comments from Stroustrup, except the one inside the parentheses.
I have no idea what it could be.
It seems like a conditional ternary operator without the first member (and without the "?"), but in that case I don't understand how it could work without a condition.
c++ syntax
6
It's a compile error on my end (gcc and clang). Plus all those lines need semicolons, but still an error.
– Cruz Jean
Apr 30 at 22:19
208
Moderator Note: Please think very carefully before casting a vote to close this as a "typo" question. Yes, the problem is a typo, but it's not a typo that the asker made. Rather, it is one found in a published book. That means this question and its answers may well be useful to others in the future, which is a strong counter-indicator for closing it as a typo. (UPDATE: This topic is now being discussed on Meta; please feel free to weigh in there.)
– Cody Gray♦
May 1 at 1:11
3
Perhaps the best answer would be: Try to compile the code; if it doesn't compile, that's a good indication that it's a typo.
– John Wiersba
May 2 at 18:41
I can think of a number of examples off the top of my head that fail to compile (or even cause an internal compiler error) on one compiler, but are accepted without issue on a different one
– J. Antonio Perez
May 3 at 0:38
1
@John I just tried some fold expressions with MSVC and they didn't compile. So clearly the whole chapter I just read must be a typo? ;) C++ compilers fail to compile valid C++ code all the time, comes from the language being absurdly complicated.
– Voo
May 3 at 7:13
|
show 4 more comments
I don't understand one particular use of a colon.
I found it in the book "The C++ programming language" by Bjarne Stroustrup, 4th edition, section 11.4.4, page 297:
void g(double y)
[&] f(y); // return type is void
auto z1 = [=](int x) return x+y; // return type is double
auto z2 = [=,y] if (y) return 1; else return 2; // error : body too complicated
// for return type deduction
auto z3 =[y]() return 1 : 2; // (Me: HERE!!!) return type is int
auto z4 = [=,y]()−>int if (y) return 1; else return 2; // OK: explicit return type
All comments from Stroustrup, except the one inside the parentheses.
I have no idea what it could be.
It seems like a conditional ternary operator without the first member (and without the "?"), but in that case I don't understand how it could work without a condition.
c++ syntax
I don't understand one particular use of a colon.
I found it in the book "The C++ programming language" by Bjarne Stroustrup, 4th edition, section 11.4.4, page 297:
void g(double y)
[&] f(y); // return type is void
auto z1 = [=](int x) return x+y; // return type is double
auto z2 = [=,y] if (y) return 1; else return 2; // error : body too complicated
// for return type deduction
auto z3 =[y]() return 1 : 2; // (Me: HERE!!!) return type is int
auto z4 = [=,y]()−>int if (y) return 1; else return 2; // OK: explicit return type
All comments from Stroustrup, except the one inside the parentheses.
I have no idea what it could be.
It seems like a conditional ternary operator without the first member (and without the "?"), but in that case I don't understand how it could work without a condition.
c++ syntax
c++ syntax
edited May 3 at 0:58
ᆼᆺᆼ
9,31643567
9,31643567
asked Apr 30 at 22:16
PiockñecPiockñec
766158
766158
6
It's a compile error on my end (gcc and clang). Plus all those lines need semicolons, but still an error.
– Cruz Jean
Apr 30 at 22:19
208
Moderator Note: Please think very carefully before casting a vote to close this as a "typo" question. Yes, the problem is a typo, but it's not a typo that the asker made. Rather, it is one found in a published book. That means this question and its answers may well be useful to others in the future, which is a strong counter-indicator for closing it as a typo. (UPDATE: This topic is now being discussed on Meta; please feel free to weigh in there.)
– Cody Gray♦
May 1 at 1:11
3
Perhaps the best answer would be: Try to compile the code; if it doesn't compile, that's a good indication that it's a typo.
– John Wiersba
May 2 at 18:41
I can think of a number of examples off the top of my head that fail to compile (or even cause an internal compiler error) on one compiler, but are accepted without issue on a different one
– J. Antonio Perez
May 3 at 0:38
1
@John I just tried some fold expressions with MSVC and they didn't compile. So clearly the whole chapter I just read must be a typo? ;) C++ compilers fail to compile valid C++ code all the time, comes from the language being absurdly complicated.
– Voo
May 3 at 7:13
|
show 4 more comments
6
It's a compile error on my end (gcc and clang). Plus all those lines need semicolons, but still an error.
– Cruz Jean
Apr 30 at 22:19
208
Moderator Note: Please think very carefully before casting a vote to close this as a "typo" question. Yes, the problem is a typo, but it's not a typo that the asker made. Rather, it is one found in a published book. That means this question and its answers may well be useful to others in the future, which is a strong counter-indicator for closing it as a typo. (UPDATE: This topic is now being discussed on Meta; please feel free to weigh in there.)
– Cody Gray♦
May 1 at 1:11
3
Perhaps the best answer would be: Try to compile the code; if it doesn't compile, that's a good indication that it's a typo.
– John Wiersba
May 2 at 18:41
I can think of a number of examples off the top of my head that fail to compile (or even cause an internal compiler error) on one compiler, but are accepted without issue on a different one
– J. Antonio Perez
May 3 at 0:38
1
@John I just tried some fold expressions with MSVC and they didn't compile. So clearly the whole chapter I just read must be a typo? ;) C++ compilers fail to compile valid C++ code all the time, comes from the language being absurdly complicated.
– Voo
May 3 at 7:13
6
6
It's a compile error on my end (gcc and clang). Plus all those lines need semicolons, but still an error.
– Cruz Jean
Apr 30 at 22:19
It's a compile error on my end (gcc and clang). Plus all those lines need semicolons, but still an error.
– Cruz Jean
Apr 30 at 22:19
208
208
Moderator Note: Please think very carefully before casting a vote to close this as a "typo" question. Yes, the problem is a typo, but it's not a typo that the asker made. Rather, it is one found in a published book. That means this question and its answers may well be useful to others in the future, which is a strong counter-indicator for closing it as a typo. (UPDATE: This topic is now being discussed on Meta; please feel free to weigh in there.)
– Cody Gray♦
May 1 at 1:11
Moderator Note: Please think very carefully before casting a vote to close this as a "typo" question. Yes, the problem is a typo, but it's not a typo that the asker made. Rather, it is one found in a published book. That means this question and its answers may well be useful to others in the future, which is a strong counter-indicator for closing it as a typo. (UPDATE: This topic is now being discussed on Meta; please feel free to weigh in there.)
– Cody Gray♦
May 1 at 1:11
3
3
Perhaps the best answer would be: Try to compile the code; if it doesn't compile, that's a good indication that it's a typo.
– John Wiersba
May 2 at 18:41
Perhaps the best answer would be: Try to compile the code; if it doesn't compile, that's a good indication that it's a typo.
– John Wiersba
May 2 at 18:41
I can think of a number of examples off the top of my head that fail to compile (or even cause an internal compiler error) on one compiler, but are accepted without issue on a different one
– J. Antonio Perez
May 3 at 0:38
I can think of a number of examples off the top of my head that fail to compile (or even cause an internal compiler error) on one compiler, but are accepted without issue on a different one
– J. Antonio Perez
May 3 at 0:38
1
1
@John I just tried some fold expressions with MSVC and they didn't compile. So clearly the whole chapter I just read must be a typo? ;) C++ compilers fail to compile valid C++ code all the time, comes from the language being absurdly complicated.
– Voo
May 3 at 7:13
@John I just tried some fold expressions with MSVC and they didn't compile. So clearly the whole chapter I just read must be a typo? ;) C++ compilers fail to compile valid C++ code all the time, comes from the language being absurdly complicated.
– Voo
May 3 at 7:13
|
show 4 more comments
3 Answers
3
active
oldest
votes
It's a typo. Look at Errata for 2nd and 3rd printings of The C++ Programming Language. The example must be like below:
auto z3 =[y]() return (y) ? 1 : 2;
10
Why(y)and not justy?
– Little Helper
May 1 at 12:36
6
@LittleHelper Perhaps its a best practice or something, I always see it written like that. Maybe to avoid confusion with more complicated comparisons...
– Redwolf Programs
May 1 at 13:02
27
Personally, I often use(cond) ? a : bfor clarity -- it helps me avoid misreading e.g. the statementfoo = x > y ? a : basfoo = x ...when skimming through code.
– grawity
May 1 at 13:10
7
@LittleHelper It's not really needed there. However in a function-like macro it's best practise to put parentheses round the arguments where they are used, because otherwise expansion of the arguments can give unexpected behaviour. Consider a function-like macro to double a value "foo(x) x * 2" where you call it with "foo(2+3)". The result will be 2+(3*2) because the argument gets expanded as-is and precedence rules take over. If your macro is "foo(x) (x)*2" then you will correctly get (2+3)*2. It may be that Stroustrup has a habit of using that style everywhere for coding safety.
– Graham
May 2 at 15:50
2
@Graham Very unlikely. Stroustrup essentially doesn't write function macros (C++ inline functions are better). Much more likely is that the ternary operator has somewhat complicated precedence rules, so it is good to habitually clarify the precedence with parens.
– Martin Bonner
May 3 at 7:44
|
show 2 more comments
Looks to me like a simple typo. Should probably be:
auto z3 =[y]() return y ? 1 : 2;
Note that since the lambda doesn't take any parameters, the parens are optional. You could use this instead, if you preferred:
auto z3 =[y] return y ? 1 : 2;
add a comment |
return 1 : 2; is a syntax error, it is not valid code.
A correct statement would be more like return (y) ? 1 : 2; instead.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
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%2f55929336%2fin-stroustrups-example-what-does-this-colon-mean-in-return-1-2-its-not-a%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
It's a typo. Look at Errata for 2nd and 3rd printings of The C++ Programming Language. The example must be like below:
auto z3 =[y]() return (y) ? 1 : 2;
10
Why(y)and not justy?
– Little Helper
May 1 at 12:36
6
@LittleHelper Perhaps its a best practice or something, I always see it written like that. Maybe to avoid confusion with more complicated comparisons...
– Redwolf Programs
May 1 at 13:02
27
Personally, I often use(cond) ? a : bfor clarity -- it helps me avoid misreading e.g. the statementfoo = x > y ? a : basfoo = x ...when skimming through code.
– grawity
May 1 at 13:10
7
@LittleHelper It's not really needed there. However in a function-like macro it's best practise to put parentheses round the arguments where they are used, because otherwise expansion of the arguments can give unexpected behaviour. Consider a function-like macro to double a value "foo(x) x * 2" where you call it with "foo(2+3)". The result will be 2+(3*2) because the argument gets expanded as-is and precedence rules take over. If your macro is "foo(x) (x)*2" then you will correctly get (2+3)*2. It may be that Stroustrup has a habit of using that style everywhere for coding safety.
– Graham
May 2 at 15:50
2
@Graham Very unlikely. Stroustrup essentially doesn't write function macros (C++ inline functions are better). Much more likely is that the ternary operator has somewhat complicated precedence rules, so it is good to habitually clarify the precedence with parens.
– Martin Bonner
May 3 at 7:44
|
show 2 more comments
It's a typo. Look at Errata for 2nd and 3rd printings of The C++ Programming Language. The example must be like below:
auto z3 =[y]() return (y) ? 1 : 2;
10
Why(y)and not justy?
– Little Helper
May 1 at 12:36
6
@LittleHelper Perhaps its a best practice or something, I always see it written like that. Maybe to avoid confusion with more complicated comparisons...
– Redwolf Programs
May 1 at 13:02
27
Personally, I often use(cond) ? a : bfor clarity -- it helps me avoid misreading e.g. the statementfoo = x > y ? a : basfoo = x ...when skimming through code.
– grawity
May 1 at 13:10
7
@LittleHelper It's not really needed there. However in a function-like macro it's best practise to put parentheses round the arguments where they are used, because otherwise expansion of the arguments can give unexpected behaviour. Consider a function-like macro to double a value "foo(x) x * 2" where you call it with "foo(2+3)". The result will be 2+(3*2) because the argument gets expanded as-is and precedence rules take over. If your macro is "foo(x) (x)*2" then you will correctly get (2+3)*2. It may be that Stroustrup has a habit of using that style everywhere for coding safety.
– Graham
May 2 at 15:50
2
@Graham Very unlikely. Stroustrup essentially doesn't write function macros (C++ inline functions are better). Much more likely is that the ternary operator has somewhat complicated precedence rules, so it is good to habitually clarify the precedence with parens.
– Martin Bonner
May 3 at 7:44
|
show 2 more comments
It's a typo. Look at Errata for 2nd and 3rd printings of The C++ Programming Language. The example must be like below:
auto z3 =[y]() return (y) ? 1 : 2;
It's a typo. Look at Errata for 2nd and 3rd printings of The C++ Programming Language. The example must be like below:
auto z3 =[y]() return (y) ? 1 : 2;
edited Apr 30 at 22:48
answered Apr 30 at 22:21
S.M.S.M.
7,39152232
7,39152232
10
Why(y)and not justy?
– Little Helper
May 1 at 12:36
6
@LittleHelper Perhaps its a best practice or something, I always see it written like that. Maybe to avoid confusion with more complicated comparisons...
– Redwolf Programs
May 1 at 13:02
27
Personally, I often use(cond) ? a : bfor clarity -- it helps me avoid misreading e.g. the statementfoo = x > y ? a : basfoo = x ...when skimming through code.
– grawity
May 1 at 13:10
7
@LittleHelper It's not really needed there. However in a function-like macro it's best practise to put parentheses round the arguments where they are used, because otherwise expansion of the arguments can give unexpected behaviour. Consider a function-like macro to double a value "foo(x) x * 2" where you call it with "foo(2+3)". The result will be 2+(3*2) because the argument gets expanded as-is and precedence rules take over. If your macro is "foo(x) (x)*2" then you will correctly get (2+3)*2. It may be that Stroustrup has a habit of using that style everywhere for coding safety.
– Graham
May 2 at 15:50
2
@Graham Very unlikely. Stroustrup essentially doesn't write function macros (C++ inline functions are better). Much more likely is that the ternary operator has somewhat complicated precedence rules, so it is good to habitually clarify the precedence with parens.
– Martin Bonner
May 3 at 7:44
|
show 2 more comments
10
Why(y)and not justy?
– Little Helper
May 1 at 12:36
6
@LittleHelper Perhaps its a best practice or something, I always see it written like that. Maybe to avoid confusion with more complicated comparisons...
– Redwolf Programs
May 1 at 13:02
27
Personally, I often use(cond) ? a : bfor clarity -- it helps me avoid misreading e.g. the statementfoo = x > y ? a : basfoo = x ...when skimming through code.
– grawity
May 1 at 13:10
7
@LittleHelper It's not really needed there. However in a function-like macro it's best practise to put parentheses round the arguments where they are used, because otherwise expansion of the arguments can give unexpected behaviour. Consider a function-like macro to double a value "foo(x) x * 2" where you call it with "foo(2+3)". The result will be 2+(3*2) because the argument gets expanded as-is and precedence rules take over. If your macro is "foo(x) (x)*2" then you will correctly get (2+3)*2. It may be that Stroustrup has a habit of using that style everywhere for coding safety.
– Graham
May 2 at 15:50
2
@Graham Very unlikely. Stroustrup essentially doesn't write function macros (C++ inline functions are better). Much more likely is that the ternary operator has somewhat complicated precedence rules, so it is good to habitually clarify the precedence with parens.
– Martin Bonner
May 3 at 7:44
10
10
Why
(y) and not just y?– Little Helper
May 1 at 12:36
Why
(y) and not just y?– Little Helper
May 1 at 12:36
6
6
@LittleHelper Perhaps its a best practice or something, I always see it written like that. Maybe to avoid confusion with more complicated comparisons...
– Redwolf Programs
May 1 at 13:02
@LittleHelper Perhaps its a best practice or something, I always see it written like that. Maybe to avoid confusion with more complicated comparisons...
– Redwolf Programs
May 1 at 13:02
27
27
Personally, I often use
(cond) ? a : b for clarity -- it helps me avoid misreading e.g. the statement foo = x > y ? a : b as foo = x ... when skimming through code.– grawity
May 1 at 13:10
Personally, I often use
(cond) ? a : b for clarity -- it helps me avoid misreading e.g. the statement foo = x > y ? a : b as foo = x ... when skimming through code.– grawity
May 1 at 13:10
7
7
@LittleHelper It's not really needed there. However in a function-like macro it's best practise to put parentheses round the arguments where they are used, because otherwise expansion of the arguments can give unexpected behaviour. Consider a function-like macro to double a value "foo(x) x * 2" where you call it with "foo(2+3)". The result will be 2+(3*2) because the argument gets expanded as-is and precedence rules take over. If your macro is "foo(x) (x)*2" then you will correctly get (2+3)*2. It may be that Stroustrup has a habit of using that style everywhere for coding safety.
– Graham
May 2 at 15:50
@LittleHelper It's not really needed there. However in a function-like macro it's best practise to put parentheses round the arguments where they are used, because otherwise expansion of the arguments can give unexpected behaviour. Consider a function-like macro to double a value "foo(x) x * 2" where you call it with "foo(2+3)". The result will be 2+(3*2) because the argument gets expanded as-is and precedence rules take over. If your macro is "foo(x) (x)*2" then you will correctly get (2+3)*2. It may be that Stroustrup has a habit of using that style everywhere for coding safety.
– Graham
May 2 at 15:50
2
2
@Graham Very unlikely. Stroustrup essentially doesn't write function macros (C++ inline functions are better). Much more likely is that the ternary operator has somewhat complicated precedence rules, so it is good to habitually clarify the precedence with parens.
– Martin Bonner
May 3 at 7:44
@Graham Very unlikely. Stroustrup essentially doesn't write function macros (C++ inline functions are better). Much more likely is that the ternary operator has somewhat complicated precedence rules, so it is good to habitually clarify the precedence with parens.
– Martin Bonner
May 3 at 7:44
|
show 2 more comments
Looks to me like a simple typo. Should probably be:
auto z3 =[y]() return y ? 1 : 2;
Note that since the lambda doesn't take any parameters, the parens are optional. You could use this instead, if you preferred:
auto z3 =[y] return y ? 1 : 2;
add a comment |
Looks to me like a simple typo. Should probably be:
auto z3 =[y]() return y ? 1 : 2;
Note that since the lambda doesn't take any parameters, the parens are optional. You could use this instead, if you preferred:
auto z3 =[y] return y ? 1 : 2;
add a comment |
Looks to me like a simple typo. Should probably be:
auto z3 =[y]() return y ? 1 : 2;
Note that since the lambda doesn't take any parameters, the parens are optional. You could use this instead, if you preferred:
auto z3 =[y] return y ? 1 : 2;
Looks to me like a simple typo. Should probably be:
auto z3 =[y]() return y ? 1 : 2;
Note that since the lambda doesn't take any parameters, the parens are optional. You could use this instead, if you preferred:
auto z3 =[y] return y ? 1 : 2;
answered Apr 30 at 22:21
Jerry CoffinJerry Coffin
391k52477926
391k52477926
add a comment |
add a comment |
return 1 : 2; is a syntax error, it is not valid code.
A correct statement would be more like return (y) ? 1 : 2; instead.
add a comment |
return 1 : 2; is a syntax error, it is not valid code.
A correct statement would be more like return (y) ? 1 : 2; instead.
add a comment |
return 1 : 2; is a syntax error, it is not valid code.
A correct statement would be more like return (y) ? 1 : 2; instead.
return 1 : 2; is a syntax error, it is not valid code.
A correct statement would be more like return (y) ? 1 : 2; instead.
answered Apr 30 at 22:21
Remy LebeauRemy Lebeau
348k19273469
348k19273469
add a comment |
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.
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%2f55929336%2fin-stroustrups-example-what-does-this-colon-mean-in-return-1-2-its-not-a%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
6
It's a compile error on my end (gcc and clang). Plus all those lines need semicolons, but still an error.
– Cruz Jean
Apr 30 at 22:19
208
Moderator Note: Please think very carefully before casting a vote to close this as a "typo" question. Yes, the problem is a typo, but it's not a typo that the asker made. Rather, it is one found in a published book. That means this question and its answers may well be useful to others in the future, which is a strong counter-indicator for closing it as a typo. (UPDATE: This topic is now being discussed on Meta; please feel free to weigh in there.)
– Cody Gray♦
May 1 at 1:11
3
Perhaps the best answer would be: Try to compile the code; if it doesn't compile, that's a good indication that it's a typo.
– John Wiersba
May 2 at 18:41
I can think of a number of examples off the top of my head that fail to compile (or even cause an internal compiler error) on one compiler, but are accepted without issue on a different one
– J. Antonio Perez
May 3 at 0:38
1
@John I just tried some fold expressions with MSVC and they didn't compile. So clearly the whole chapter I just read must be a typo? ;) C++ compilers fail to compile valid C++ code all the time, comes from the language being absurdly complicated.
– Voo
May 3 at 7:13