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

Show parent comments

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); //...
}

1

u/[deleted] Apr 02 '13

[removed] — view removed comment

2

u/nascent Apr 02 '13

Well, I'm pretty sure it will be the same if you have a class with 10 fields vs a class with 20 fields.

As for unneeded or poorly sized, I don't think the type will give you any of that information. And for poorly sized that would mean the type was poorly sized so you're back to the type definition.

From my experience type definitions don't use auto or typeof very much.

2

u/[deleted] Apr 02 '13

[removed] — view removed comment

1

u/nascent Apr 02 '13

And of course if a class has 20 fields, I damn well question what the hell the class is doing. ;) That is way too much state for me to be comfortable with!

That was my point, it wasn't knowing the fields were UINT32 and UINT64 that told you that you could remove them.

The question is, why did you decide you need to reduce the size of the struct? Did you see the 128 bits and say, "well that's too much?" Or did you notice, "I have a large number of this struct being sent to me in a short time, maybe I should make it smaller?"