lundi 1 juin 2015

Is "trivially copyable" enough for the same passing conventions as a raw pointer?

I want my custom Ptr<T> pointer class to produce equivalent assembly as a raw pointer T *. In particular, when passing in and out of functions, I want it to be passed in registers (just like T * is), not on stack.

By experimenting, I found out that at least gcc 4.9.2 requires the Ptr<T> to be trivially copyable, then it produces identical assembly as raw pointer for all situations that I tried (using registers). If it is not trivially copyable, then it uses stack, not registers.

It doesn't seem to be required to be a standard layout type nor trivial (i.e. having a trivial default constructor in addition to being trivially copyable).

Can I count on this on all platforms or is this compiler dependent?

What are the recommendations for a custom class (in terms of the traits above), so that it is equivalent to raw pointers in terms of the generated assembly code (when optimizations are on)?

Aucun commentaire:

Enregistrer un commentaire