News for hackers. Stuff that matters.
Re:C++ Internals: STL Vector, Part 3 by overgard on Sunday August 09, 2015 @04:11PM (#10031396)
I know this is a nitpick, but "auto main() -> int" seems weird to me. "int main()" is not only more understandable, but also shorter.
Re:C++ Internals: STL Vector, Part 3 by StephanTLavavej on Sunday August 09, 2015 @04:39PM (#10031498)
Yep, that's a totally counterproductive use of trailing return types. There are several reasons to use trailing return types, but that's not one of them. (The major use is when you have to refer to function parameters with decltype. Additional uses are when the return type would have to be qualified on the left but can be shorter on the right, or when you're returning something like a function pointer or reference to array and you don't have a convenient typedef/alias.)
Re:C++ Internals: STL Vector, Part 3 by detrino on Sunday August 09, 2015 @08:19PM (#10032061)
One could argue that trailing return types are more consistent and allow you to switch between return type deduction and explicit return types easier.
Re:C++ Internals: STL Vector, Part 3 by byuu on Monday August 10, 2015 @12:18AM (#10032618)
In addition, it's also more consistent with lambda syntax, lets you use decltype on parameter arguments, allows your function names to line up nicely in header files, and my favorite feature, is scoped for member functions. Ex:

    MessageWindow::Response MessageWindow::question(...);
    MessageWindow::Icon MessageWindow::getIcon() { return _icon; }
    ...
    auto MessageWindow::question(...) -> Response;
    auto MessageWindow::getIcon() { return _icon; }
I was fairly nervous about it at first, but after using this syntax, I've grown to highly favor it. Being a huge fan of consistency, I've begun using only this style. Downside is it seems to annoy many other programmers much moreso than other style choices (like const foo vs foo const, tabs vs spaces, K&R vs 1TBS, etc.)

I do kind of wish you could say "function" instead of "auto" here, but of course that wasn't a reserved keyword so it would have broken existing code.

For the ubiquitous main, "auto main() {}" return type deduction works pretty well. But I need a wrapper around main() anyway thanks to Windows corrupting argv[] when you pass in non-ANSI (Unicode) characters. So for my case, I have:

    #include <nall/main.hpp>
    auto nall::main(vector<string> args) { ... }
Where internally, the real main() will use CommandLineToArgvW + WideCharToMultiByte on Windows to return UTF-8 arguments. So many cross-platform apps skip this step, and make opening files named in other languages impossible via command-line.
Re:C++ Internals: STL Vector, Part 3 by AdieuToLogic on Monday August 10, 2015 @12:30AM (#10032656)
> In addition, it's also more consistent with lambda syntax, lets you use decltype on parameter arguments, allows your function names to line up nicely in header files...

Great points for the repurposing of auto present in the latest standard... Not for use in defining main.

Your case regarding Windows and wrapping main I'm sure is valid. But is not the case with the article's definition.

Re:C++ Internals: STL Vector, Part 3 by detrino on Monday August 10, 2015 @04:09AM (#10033283)
I didn't realize it was scoped this way, that's a nice bonus.
Re:C++ Internals: STL Vector, Part 3 by AdieuToLogic on Sunday August 09, 2015 @11:40PM (#10032534)
One could present that argument, yes, but would be questioned as to why the argument is needed for the canonical main function definition.

Usage of this syntax for main not only deviates from expected form, its presence in defining the one function every C++ developer knows by heart is self-defeating.

Re:C++ Internals: STL Vector, Part 3 by detrino on Monday August 10, 2015 @03:44AM (#10033198)
What a long winded way to say "that syntax is weird" while totally ignoring that my main point was consistency.
Re:C++ Internals: STL Vector, Part 3 by andrepd on Sunday August 09, 2015 @05:50PM (#10031709)
And just a few lines below that: "for (auto item : { 1, 2, 3, 4, 5, 6, 7, 8, 9 })". Why?? It's an almost comical abuse of C++11 for the sake of it.
Re:C++ Internals: STL Vector, Part 3 by nadams on Sunday August 09, 2015 @08:01PM (#10032015)
> And just a few lines below that: "for (auto item : { 1, 2, 3, 4, 5, 6, 7, 8, 9 })". Why?? It's an almost comical abuse of C++11 for the sake of it.

This comment is very interesting. His syntax reminds me of Python syntax of:

    for i in [1,2,3]:
Isn't that what the future C++ is trying to achieve? Simplified syntax?
Re:C++ Internals: STL Vector, Part 3 by srean on Sunday August 09, 2015 @08:51PM (#10032163)
> Isn't that what the future C++ is trying to achieve? Simplified syntax?

I think your question was rhetorical, in any case the answer is yes. In fact the proposed C++ pythy syntax tries go even further http://pfultz2.github.io/Pythy/ . IIRC its Clang only. Its author hangs out on HN sometimes and would be able to provide more information

Re:C++ Internals: STL Vector, Part 3 by detrino on Sunday August 09, 2015 @08:20PM (#10032063)
It's worse in every way to:

    for (int i = 1; i != 10; ++i)
Re:C++ Internals: STL Vector, Part 3 by andrepd on Monday August 10, 2015 @11:32AM (#10035233)
Even in that example, wouldn't it be better to simply write

    for i in range(1,4):
Re:C++ Internals: STL Vector, Part 3 by sjolsen on Monday August 10, 2015 @03:24AM (#10033145)
Why? Because that's the most concise way in general to iterate over an arbitrary list of enumerated values. There's no "abuse" here, just a particular example that could have been written more concisely.
Re:C++ Internals: STL Vector, Part 3 by fauigerzigerk on Monday August 10, 2015 @04:14AM (#10033295)
It's not the most concise way to iterate over a sequence of numbers from 1 to 9. Worse than that, it suggests that there may be a number missing somewhere in the middle. You have to scan the entire sequence with your eyes to be sure.

  for (int i = 1; i <= 9; ++i)
is both more concise and requires less thinking to know what's going on.
Re:C++ Internals: STL Vector, Part 3 by sjolsen on Monday August 10, 2015 @07:00PM (#10038027)
That the data he chose can be expressed as a range is utterly inconsequential. The values are never even used. In all likelihood, the author just wrote

    for (auto item : {})
and then filled in the first sequence of numbers that popped into his head.
Re:C++ Internals: STL Vector, Part 3 by fauigerzigerk on Tuesday August 11, 2015 @08:40AM (#10040531)
>That the data he chose can be expressed as a range is utterly inconsequential

It could be, but we don't know that. And that's exactly my point. The syntax he uses forces us to think and even speculate about that. So for the particular sequence he chose for his example, it would be less ambiguous to generate the range algorithmically.

Re:C++ Internals: STL Vector, Part 3 by emmelaich on Monday August 10, 2015 @06:31AM (#10033709)
The point of using this sort of syntax for scrapcode is that you can just textually replace the the {...} with a real container variable.

If you should be so inclined later.

Re:C++ Internals: STL Vector, Part 3 by gahcep on Monday August 10, 2015 @07:09AM (#10033809)
Damn, too much confusion was made. I didn't pay enough attention to these things, my bad. Got rid of it.
Re:C++ Internals: STL Vector, Part 3 by thrownaway2424 on Sunday August 09, 2015 @05:12PM (#10031610)
Couldn't it even just have been "auto main()" and nothing in c++14?
Re:C++ Internals: STL Vector, Part 3 by StephanTLavavej on Sunday August 09, 2015 @06:03PM (#10031741)
No, that was forbidden by the resolution of Core Issue 1669, see: http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html...