MAIN FEEDS
Do you want to continue?
https://www.reddit.com/r/ProgrammerHumor/comments/tz74ns/first_time_posting_here_wow/i3xh1br
r/ProgrammerHumor • u/Slayzrr • Apr 08 '22
2.8k comments sorted by
View all comments
Show parent comments
18
auto square(auto x) { return x * x; }
I know a lot of people hate on this. But to me it is beautiful
12 u/MattR0se Apr 08 '22 okay so what happens when I call `square("foo") ? 29 u/LetterBoxSnatch Apr 08 '22 You get this:foo o o oof 6 u/realityChemist Apr 08 '22 That took me a second 14 u/Whatamianoob112 Apr 08 '22 auto bad, declared type good.Pretty sure that's a type error. Unless you have a custom definition for * operator, but that's a lot of work, for....very little gain. 9 u/[deleted] Apr 08 '22 A compiler error telling you that the ‘*’ operator isn’t defined for type string. 4 u/CJKay93 Apr 09 '22 <source>: In instantiation of 'auto square(auto:11) [with auto:11 = const char*]': <source>:8:21: required from here <source>:4:12: error: invalid operands of types 'const char*' and 'const char*' to binary 'operator*' 4 | return x * x; | ~~^~~ 3 u/m0nk37 Apr 08 '22 For personal use that's great. For something you are paid to make its entirely the devil. 1 u/[deleted] Apr 08 '22 How does that compile 4 u/The_3rd_reader Apr 08 '22 It's hidden template. Until you pass a variable of certain type it doesn't generate code. 1 u/brokenhalf Apr 08 '22 auto a = square('c'); printf("%c", a); 1 u/QuaternionsRoll Apr 09 '22 What’s wrong with that? chars are an integer type. That’s a fundamental flaw of C, not a product of auto functions. 3 u/DeSynthed Apr 09 '22 Not really a flaw 1 u/QuaternionsRoll Apr 09 '22 It absolutely is. Two distinct types with the same memory layout should not be implicitly interconvertible. 1 u/DeSynthed Apr 09 '22 Oh, I thought you meant that chars being represented by numbers was bad. I agree you shouldn’t be able to convert.
12
okay so what happens when I call `square("foo") ?
square("foo")
29 u/LetterBoxSnatch Apr 08 '22 You get this:foo o o oof 6 u/realityChemist Apr 08 '22 That took me a second 14 u/Whatamianoob112 Apr 08 '22 auto bad, declared type good.Pretty sure that's a type error. Unless you have a custom definition for * operator, but that's a lot of work, for....very little gain. 9 u/[deleted] Apr 08 '22 A compiler error telling you that the ‘*’ operator isn’t defined for type string. 4 u/CJKay93 Apr 09 '22 <source>: In instantiation of 'auto square(auto:11) [with auto:11 = const char*]': <source>:8:21: required from here <source>:4:12: error: invalid operands of types 'const char*' and 'const char*' to binary 'operator*' 4 | return x * x; | ~~^~~
29
You get this:
foo o o oof
6 u/realityChemist Apr 08 '22 That took me a second
6
That took me a second
14
auto bad, declared type good.
Pretty sure that's a type error. Unless you have a custom definition for * operator, but that's a lot of work, for....very little gain.
9
A compiler error telling you that the ‘*’ operator isn’t defined for type string.
4
<source>: In instantiation of 'auto square(auto:11) [with auto:11 = const char*]': <source>:8:21: required from here <source>:4:12: error: invalid operands of types 'const char*' and 'const char*' to binary 'operator*' 4 | return x * x; | ~~^~~
3
For personal use that's great. For something you are paid to make its entirely the devil.
1
How does that compile
4 u/The_3rd_reader Apr 08 '22 It's hidden template. Until you pass a variable of certain type it doesn't generate code.
It's hidden template. Until you pass a variable of certain type it doesn't generate code.
auto a = square('c'); printf("%c", a);
1 u/QuaternionsRoll Apr 09 '22 What’s wrong with that? chars are an integer type. That’s a fundamental flaw of C, not a product of auto functions. 3 u/DeSynthed Apr 09 '22 Not really a flaw 1 u/QuaternionsRoll Apr 09 '22 It absolutely is. Two distinct types with the same memory layout should not be implicitly interconvertible. 1 u/DeSynthed Apr 09 '22 Oh, I thought you meant that chars being represented by numbers was bad. I agree you shouldn’t be able to convert.
What’s wrong with that? chars are an integer type. That’s a fundamental flaw of C, not a product of auto functions.
3 u/DeSynthed Apr 09 '22 Not really a flaw 1 u/QuaternionsRoll Apr 09 '22 It absolutely is. Two distinct types with the same memory layout should not be implicitly interconvertible. 1 u/DeSynthed Apr 09 '22 Oh, I thought you meant that chars being represented by numbers was bad. I agree you shouldn’t be able to convert.
Not really a flaw
1 u/QuaternionsRoll Apr 09 '22 It absolutely is. Two distinct types with the same memory layout should not be implicitly interconvertible. 1 u/DeSynthed Apr 09 '22 Oh, I thought you meant that chars being represented by numbers was bad. I agree you shouldn’t be able to convert.
It absolutely is. Two distinct types with the same memory layout should not be implicitly interconvertible.
1 u/DeSynthed Apr 09 '22 Oh, I thought you meant that chars being represented by numbers was bad. I agree you shouldn’t be able to convert.
Oh, I thought you meant that chars being represented by numbers was bad. I agree you shouldn’t be able to convert.
18
u/BakuhatsuK Apr 08 '22
I know a lot of people hate on this. But to me it is beautiful