r/programming • • Apr 01 '13

Ten C++11 Features Every C++ Developer Should Use

http://www.codeproject.com/Articles/570638/Ten-Cplusplus11-Features-Every-Cplusplus-Developer
469 Upvotes

283 comments sorted by

View all comments

0

u/[deleted] Apr 01 '13

[removed] — view removed comment

9

u/rabidcow Apr 01 '13

I want to be able to scan down the left hand side of a class declaration and quickly see how much memory that class is going to use when instantiated

That's fine, though. auto can't be used there. You may have a problem if you need to know how much stack a function uses, but that's nontrivial anyway.

I'd agree that it's usually a bad idea to use auto for basic types, but if I have to use an iterator, that's gonna be auto.

9

u/[deleted] Apr 01 '13 edited Dec 09 '16

[removed] — view removed comment

→ More replies (4)
→ More replies (1)

5

u/uututhrwa Apr 01 '13

What a dumb comment. What about all the implicit conversions? Or the fact that a class having value members of other classes would require scanning source files anyway. Can't you write some unit test or IDE plugin calling sizeof if that's your thing? Or not using it at all since you still have the option. I am not even sure if you can actually use auto on class declarations like that (you can't in C#, where the equivalent var keyword has caused problems, nowhere, and made some tedious to write code a lot easier). In some languages that use auto generalization of code ( Ocaml?) the question might be relevant but in C++ it isn't.

27

u/wilhelmtell Apr 01 '13

I'm in Sutter's camp on this one. Rotate your eyeballs slightly to the right and you'll find the type you're looking for. 42 is an int, and if you don't know that then review your C++. For more complex expressions, like

auto splines = reticulate_splines();

it's still a better idea to use auto than to explicitly state the type because you then know there is no conversion. Stating the type of splines will not tell you the return-value type of reticulate_splines(); it will only tell you that there may or may not be an implicit conversion going on here. This "may or may not" thing is a problem.

5

u/sol_aries Apr 01 '13

Except if you actually look up reticulate_spline and explicitly type it, you are saving the untold number of people that come after you from having to do the same (including yourself).

12

u/wilhelmtell Apr 02 '13 edited Apr 02 '13

auto is not there to save keystrokes. Occasionally it's an added bonus, sometimes you actually type more than otherwise. auto is a tool to get stuff done and to avoid traps, not a mere syntactic helper.

  • You can use auto for return-type computation as a function of the signature.

  • You can use auto as a DRY tool.

  • You can use auto to make conversions explicit. Without auto conversions in initialization were implicit and there was no way for you change that unless you could change signature on the right side of an assignment. In the case of builtins -- not even then.

If you don't know the type on the right side of an assignment you look it up. You don't duplicate stuff "to relief others from looking them up". You don't copy-paste entire friggin functions to relief yourself or your poor descendants from looking up code. You don't repeat function declarations on top of your code to relief your colleagues from looking up the signature.

→ More replies (5)
→ More replies (20)

4

u/mhenr18 Apr 02 '13

Fortunately, the sizeof() operator has existed since the dawn of time.

edit: And as rabidcow has said, you need an rvalue to use auto, which you won't get in a class declaration.

→ More replies (1)

8

u/ascii Apr 01 '13

You are old and wrong. The compiler will infer variable usage patterns and reuse variable slots that do not overlap. It may also pad some types to up performance. Type inference reduces repetition in the code, which is good. If you want to know the size of a function call stack frame, ask your debugger instead of making a bad guess. You'll find you were wrong.

2

u/nascent Apr 01 '13

I'm going to guess that your quickly is also going to be inaccurately. If your going to check size of your type you probably should be asking your compiler. I don't think C++ makes that easy, but I'd think if it really was important to know you'd have

 pragma(msg, MyClass.sizeof);

3

u/imMute Apr 01 '13

It definitely matters in situations where it must match some spec (like a hardware register sets) or you're instantiating many of them.

3

u/nascent Apr 01 '13

So why don't you have an easy way to get the size of your type? If you must maintain a specific size due to spec I would think

static assert(MyClass.sizeof == 64);

would make for better guarantees than a scan intended to be quick.

4

u/imMute Apr 02 '13

Because its not always the total size but how fields are packed into the structure.

2

u/nascent Apr 02 '13

Unittests?

unittest {
    MyClass mc;
    // Add values to mc...
    auto mcbytes = cast(ubyte[]) mc;
    assert(mcbytes[0] == 4); //...
}
→ More replies (4)

1

u/sol_aries Apr 01 '13

I wish I could upvote this more. The simple, even subconscious, process of visually parsing through code becomes a laborious one, opening a myriad of windows as you track down what's happening in there. Eg, "auto jj = foo();" on code I'm trying to get to compile (so limited ide support) means I have to track down 'foo()' to figure out what's happening. IMO, this is one terrible feature meant to cover up another.

9

u/ascii Apr 01 '13

Dynamic programming languages have shown that this is a quite feasible tradeoff for decades.

11

u/geaw Apr 02 '13

Also, I never hear C# programmers complain about var, nor Haskell programmers about type inference. This all kind of sounds like "it's different so it's bad" to me.

5

u/stusmith Apr 02 '13

Yep I remember when C# got the 'var' keyword. The debate went as follows:

  • I don't want dynamic variables!

  • Oh, it doesn't mean dynamic... why would I use it for 'int'?

  • Actually, it makes sense... use it everywhere. (*)

  • Oh look ReSharper agrees with me.

(*) - Unless a variable isn't being initialized, or needs to be typed as a different interface type.

→ More replies (5)

7

u/PurpleSfinx Apr 01 '13

Won't any halfway decent IDE show the type on a mouseover or something?

→ More replies (5)

8

u/[deleted] Apr 01 '13

[removed] — view removed comment

7

u/imMute Apr 01 '13

IMO if you're using auto then the type should literally not matter. So that's just for loops and iterators.

6

u/mb86 Apr 02 '13

How about when declaration and initialization both require typing out the full (perhaps very convoluted) type? Example I extended from someone else's above

std::shared_ptr<std::unordered_map<int,std::vector<int>>> m = std::make_shared<std::unordered_map<int,std::vector<int>>>(args);

versus

auto m = std::make_shared<std::unordered_map<int,std::vector<int>>>(args);

Here the auto makes it much cleaner and doesn't make any ambiguity when reading.

4

u/s73v3r Apr 02 '13

If you're actually caring about memory in that way, then please, don't use it. But not everyone is in the same boat.

4

u/ascii Apr 01 '13

In cases where you care about unint16 vs. uint8, it sounds like it might make a lot of sense to skip auto. Nobody is forcing you to use it everywhere. But I would say that's the exception, not the rule.

→ More replies (4)
→ More replies (3)
→ More replies (13)