0

I want to use a single-element struct purely for type safety. However, it would be desired to behave exactly like code using the underlying type including passing it directly in registers (similarly to #repr[transparent] in Rust).

I am asking about specific compilers, but of course if there is a standard way, that's even better.

(Yes, I know any differences are probably going to be trivial, but at the least this will allow me to actually measure them without rewriting a lot of code.)

Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
  • I guess it very much depends on the way how you define such a struct. Are all special member functions _trivial_? If yes, I believe a compiler will pass it in registers. Compare: https://godbolt.org/z/P4TesY4bY. More specifically, the System V ABI uses the term _"(non-)trivial for the purpose of calls"_. – Daniel Langr Oct 14 '21 at 09:51
  • Possibly relevant question: [Why can a T* be passed in register, but a unique_ptr cannot?](https://stackoverflow.com/q/58339165/580083) – Daniel Langr Oct 14 '21 at 10:07
  • "Are all special member functions trivial?" Yes. OTOH, I do care about ARM64 as well. Based on the description [here](https://learn.microsoft.com/en-us/cpp/build/arm64-windows-abi-conventions?view=msvc-160#return-values) it seems the `#repr[transparent]` doc is not quite correct and returning `struct X { double x; };` should return it in a register, not by pointer; but it uses `x0` register where returning a `double` would use `d0`. – Alexey Romanov Oct 14 '21 at 10:08
  • You can easily modify my example for ARM64 and `double`: https://godbolt.org/z/EMofj5cMT. It seems to work the same, at least with GCC. – Daniel Langr Oct 14 '21 at 10:11
  • 1
    Yes, I've tried to play with it a bit, but can't reproduce the expected difference. OTOH, I went to ARM's own documentation in https://developer.arm.com/documentation/ihi0055/b/ and it does seem to be expected, so maybe I'm misunderstanding something. Thank you anyway! – Alexey Romanov Oct 14 '21 at 10:27

0 Answers0