I don't get c++11.

This topic is 1419 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I have been programming C since long before C++, and C++ since it's birth.  The standard has evolved a lot, and it's just trying to keep up with the innovation happening elsewhere, in other langauges.

Honestly, lambda's are simpler to manage than function pointer declarations.  As with most things, it's simple, it just won't seem that way until it does for you.

I don't like some of the additions; things like auto and decltype are not expanding the capability of the language, but only helping some become more comfortable with it.  I fully expect a day where two expert C++ coders can write code that the other cannot understand :-)

Share on other sites

I don't like some of the additions; things like auto and decltype are not expanding the capability of the language, but only helping some become more comfortable with it.

Even though I agree that these are not adding up to the language, comfort plays a really important role when learning a language. I lost account of how many times I've seen people asking for help when their problem was simply misusing and 'misdeclaring' iterators... This kind of problem adds a lot to the learning curve and features like auto will help anyone who is just starting.

Share on other sites

I don't like some of the additions; things like auto and decltype are not expanding the capability of the language, but only helping some become more comfortable with it.  I fully expect a day where two expert C++ coders can write code that the other cannot understand :-)

auto is required to hold and use local lambdas, if the lambda has captures (Only non-capturing lambdas can be accessed as function-pointers, because only non-capturing lambdas are actually functions).

decltype() is used to implement auto, as well as to handle certain template situations where you have to jump through too many hoops to describe a type or where you are incapable of describing a type.

Range-based for loops are shorthand and only shorthand, and don't enable any new feature - so your ire should be directed in that direction... that is, if range-based for loops didn't enable behind-the-scenes optimizations that compilers can take advantage of.

[b][edit:][/b] Ninja'd

Edited by Servant of the Lord

Share on other sites

std::function<double(double)> truncate

That syntax in between the <>'s, is that some kind of new language extension? Not heard anything about this but doesn't seem to be something that would have been valid in pre C++-11.

Share on other sites
Maybe my coffee hasn't kicked in yet but that looks to me like the function syntax I have been using for years (starting with boost::function, nowadays std::function). It's simply std::function<ReturnType (ParameterType1, ParameterType2, ...)>.

Edit: Boost used to warn that while it's the preferred syntax not all compilers support it. But all the MSVCs I have used it with never complained. Edited by BitMaster

Share on other sites

I'm not a big fan of the way rvalue and perfect forwarding interact.  Perfect forwarding should have had a separate syntax, and not be tacked onto rvalues with unintuitive reference collapsing rules and special template deductions.  I understand the need for perfect forwarding, but it has nothing to do with rvalues... why reuse the syntax??  C++ template deduction rules are already convoluted enough as it is.  They needed to make it simpler, not more complex.

Share on other sites

Pretty comfortable with my c++ code now; I get classes, I get using templates and STL, smart pointers, and to an extent, exceptions. What I don't get is most of the new 2011 standard. Lambdas confuse me, in both their syntax and their purpose.

I've looked up articles trying to show practical uses for them, but they seem to go even further above my head than just the word 'lambda'. Rvalue / moving seems confusing just as well; decltype... gah. None of the new stuff makes much of any sense. The only thing I've understood so far is auto, and to a half-way extent: the new for loop style.

I've read through a third of C++ Primer 5th, and so far it's not really shed any new light to me. The frustration of these new features and my inability to grasp them has me concerned... How is it some clicked onto the new concepts rather fast? And where can I go to get to that point?

Other people grokked them fast because lambdas aren't new other languages already had these, lambdas in C# for example, they started as nameless delegates. I see lambdas as a delegate/callback function that you specify in the same line as where you pass them to a function that needs a callback. The lambdas in C++11 have a more function approach and I find them easier to read then their C# counterparts, its the capture that is hard.

A good use for a lambda is when you want to extend a std::find or std::find_if or std::sort, instead of passing it a functor or callback, you pass it a lambda that specifies what you want the predicate for the function to be. This keeps the code for the predicate and call of that predicate closer together than the functor or callback options.

RValue/Move semantics is something that was sorely missed and you had to program around this like reserving the size of your vector if you knew how big it was going to be to avoid an array copy. Move semantics will allow you to care a little less about copy performance if implemented correctly.

Decltype is a way in which we can access the type of an expression, this is useful in meta template programming and generic programming where you had to hack around these things before.

auto and the new intialiser lists are the most useful features added in C++11 because it allows you to write your code in an easier fashion and avoid a parsing problem.

class Foo
{
};

void Bar()
{
Foo foo(); //The compiler sees this as a function prototype not the instantiation of an instance of Foo
Foo foo {}; //This is now always seen as creating and instance of Foo
}

Edited by NightCreature83

Share on other sites

Two errors here:
1. Missing default capture mode, must specify either [&] or [=];
2. Missing ; behind lambda definition

Thanks, I usually forget the semicolons.

Share on other sites
I have grown to accept MSVCs deficiencies. I can live with them when I'm being paid.

For my personal projects I have moved over to QtCreator + MinGW though. After some initial friction (QtCreator can be a bit quirky when you are not used it) the only thing I'm actually missing is MSVC's debugger.

Share on other sites

So long as you try not to overly consume language features and kinda stick to the lowest common denominator, I find that code should remain portable with older / non-standard compilers.

The really annoying one for me was when std::shared_ptr<T> was in the tr1 namespace on some compilers (std::tr1::shared_ptr<T>) for seemingly a very long time. I had to use a macro to attempt to hack round it whilst fighting off suggestions of "use boost::shared_ptr<T>". (Javascript developers here might feel the same way when every sample code around the internet imposes jquery on your codebase).

I am looking forward to the day however when Clang is working fully on OpenBSD and I can ditch eg++ but retain the C++11 features!

Edit: Stroustrup actually summarizes my issues with boost quite well here

Edited by Karsten_