On Fri, 01 Mar 2013 17:20:00 -0500, Namespace <[email protected]> wrote:

Size is next polymorphism my main reason why I would use a class instead of a struct. A good heuristic size (which I have heard here) was: <= 16 bytes -> struct, > 16 bytes -> class.
And what are your reasons for decision?

Features of classes vs. structs. Need an interface? class. Need fine control over lifetime? struct.

There are lots of other features that can make the decision. Size is a very small part of it.


And I believe, actually, that passing a massive struct by value if it's an rvalue IS the most performant -- no copy needed, no referencing needed.

And what a massive struct do you think?
For example, if you have something like this:

struct Massy {
public:
        int[1024] marr;
}

I would bet that it is usually better to take 'Massy' by ref, or to use a class, instead of a move or a copy.

foo(Massy m)
{
   for(int i = 0; i < sizeof(m.marr); ++i)
     m.marr[i] = i;
}

main()
{
   foo(Massy());
}

OK, so let's take the case that foo accepts Massy by ref:

1. Massy has to be pushed onto the stack
2. A ref to Massy is pushed onto the stack
3. Every access to m in foo must go through a pointer dereference (there are 1024 of them)

What if Massy is accepted by value?

1. Massy has to be pushed onto the stack
2. Massy is NOT copied to pass to foo, it's passed directly, there is no copy involved! No ref is needed either
3. Every access to m does NOT need to be dereferenced.

I agree, lvalues, Massy needs to be passed by ref. But the case can be made that passing rvalues by ref is lower performance, even if slightly.

Often times though, I would think that it's not worth the trouble to have both a ref and non-ref foo. In that case, passing foo by ref is preferred, because the penalty is only slight in passing an rvalue by reference, but is humongous when passing an lvalue by value.

auto ref was supposed to be this magic feature.

-Steve

Reply via email to