I don't get c++11.

Started by
34 comments, last by tanzanite7 10 years, 1 month ago

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. wink.png

[edit:] Ninja'd ph34r.png

Advertisement

Points well taken, oh higher-than-my-reputation-people!

My mental model when programming is as though C++ is an OOP C, a higher-level abstraction for what I understand the CPU to be doing (from years of ASM work). Nonetheless, I love the STL and other abstractions, and in 11, lambda's and especially cross-platform threading! I suppose there will just be features I am unlikely to use--well, you know, until I absolutely need them--because I can solve all the problems I know using older mechanisms.

A much more mature and capable language now, C++, as a descendant of C, still makes me want to hear Kernighan's wise words: "C is not a big language, and it is not served well by a big book."

(Stroustrup made no such promises, but once upon a time, even C++ was not a big language...)

I'm curious about this that dejaime posted:

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.

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.

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.

But imagine I wanted to use a local variable of 'main()' in my lambda...


int main()
{
    int localVar = 357;
    
    auto foo = []()
    {
       return localVar;
    }

    int x = foo();
}
...lambdas can "capture" the local variable, either by value or by reference, and access it later (assuming the reference is still valid).

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


int main()
{
    int localVar = 357;
    
    auto foo = [=]()
    {
       return localVar;
    };

    int x = foo();
}

I'm curious about this that dejaime posted:

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.


It's just function type declaration minus the typename part;

typedef double (*fp_t)(double)
fp_t foo;

typedef std::function<double(double)> fp_t;
fp_t foo;
boost::function used this syntax before std::function was folded into the standard.

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
}

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

I thank everyone who had explanations on all topics. I tried a few sample lambdas to try to get a feel; and it's beginning to make sense. I'll be honest in that I never really used a callback; let alone know what one is. From my understanding, it's passing a function into another, and having it perform at a later point within that first function.

Move constructors and assignments are entirely cleared up; that actually seems like a brilliant move; and left me wondering why that wasn't part of the standard earlier.

decltype has also been cleared up; and I can see the uses for that definitely. I bookmarked the GoingNative page; and will check that out later today when I finish classes; thanks for the link!

I really do appreciate all of your guy's explanation, and time taken to give them. The depth of some of them were quite long, but served to help more than what the book had shown; so thanks to all on their contributions!

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


Thanks, I usually forget the semicolons. laugh.png

This topic is closed to new replies.

Advertisement