News for hackers. Stuff that matters.
Re:Returning multiple values from functions in C++ by imron on Sunday March 06, 2016 @09:10PM (#11236658)
That's not the fault of optional though. It's because with the optional version you're creating (and destroying) a new string each iteration of the loop.

Something like this (untested) should be much closer to the non-optional version:

    #include <iostream>
    #include <string>
    #include <boost/optional.hpp>

    using namespace std;
    using namespace boost;

    optional<string&> getline_(istream &st, string& line) {
        if(getline(st, line)) return line;
        return none;
    }   

    int main() {
        int lines = 0;
        string line;
        line.reserve( 1024 );  //1024 should be long enough for any line
        while(getline_(cin, line )) lines++;
        cout << lines << "\n";
    }
Re:Returning multiple values from functions in C++ by yoklov on Monday March 07, 2016 @09:31AM (#11239036)
Which more or less removes any benefit for using optional, unfortunately.
Re:Returning multiple values from functions in C++ by imron on Monday March 07, 2016 @08:09PM (#11242940)
Yes for this case, but that's just because it's a bad example for comparing the performance of optional, because the code is doing things that have very different performance characteristics.

There are plenty of other cases though where it is useful to use optional (e.g. instead of passing/returning a naked pointer which may or may not be null) and in those cases use of optional will have little/no overhead.