by asitdhal · 9y ago · 0 comments
http://stackoverflow.com/questions/38060436/what-are-the-new...
std::optional and std::any are one of the good things. std::apply and std::invoke can help you write better functional c++17 code.
Anyway thanks for C++17. For sometime, I thought I will learn Rust.
On x86_64 Linux, it looks like a function with signature `void f(std::variant<int, char>)` expects its (8-byte) argument to be passed by reference, whereas `void f2(tagged_union_of_int_and_char)` passes its argument in rdi.
gcc (-O3) also generates miserable code for calling f(42):
4004f0: 48 83 ec 18 sub $0x18,%rsp
4004f4: 48 89 e7 mov %rsp,%rdi
4004f7: c7 04 24 2a 00 00 00 movl $0x2a,(%rsp)
4004fe: c6 44 24 04 00 movb $0x0,0x4(%rsp)
400503: e8 c8 ff ff ff callq 4004d0 <f>
400508: 48 83 c4 18 add $0x18,%rsp
40050c: c3 retq
as compared with calling f2(42): 400510: 48 bf 00 00 00 00 2a movabs $0x2a00000000,%rdi
400517: 00 00 00
40051a: eb c4 jmp 4004e0 <f2>
std::optional seems to have the same disease; the ABI is different from a plain struct containing a bool and the value and you get worse initialisation code. There's also a build time penalty to using std::optional; on my machine, including the code that uses optionals makes my trivial test program take 320ms to compile and link rather than 50ms.These look like yet more new C++ features that are worse than what they're trying to replace.
Now why it is not trivially copy constructible is another question. But the latest standard draft does not require it [variant.ctor] though it does require the destructor to be trivial if all type destructors are trivial [variant.dtor]. The GCC implementation also seems to ensure only trivial destructor not copy constructor (the code is here [1] if anyone likes to see).
EDIT: I did a small experiment that agrees with this - adding a copy constructor causes passing by pointer. https://godbolt.org/g/UG0YXJ
EDIT2: I filed a bug report for gcc, https://gcc.gnu.org/bugzilla/show_bug.cgi?id=80187
[1] https://github.com/gcc-mirror/gcc/blob/master/libstdc%2B%2B-...
It should be possible to implement std::variant in such a way that it is trivially constructible and destructible as long as all elements are, but apparently stdlibc++ doesn't. This is GCC choice and other compilers apparently do differently. Unless GCC changes it before finalising their c++17 implementation, the choice will be unfortunately set in stone for them as they guarantee abi stability of the library.
P0602R0 is a proposal to require trivial copy/move (but strangely not destruct), don't know whether it has been accepted though.
In most cases, I imagine the performance penalty would be dwarfed by the cost of actually using the class.
void f(std::variant<int, char>)
would be f(x: IntOrChar)
roughly in Rust, I'm too lazy to make the types _exactly_ the same, but https://godbolt.org/g/pZde2A is roughly it.Actually, https://godbolt.org/g/1vcMeG might be even better to compare; I'm not an expert, but looks like the only difference is in the size.
last one, adding inline(never) so you can see the calls in main: https://godbolt.org/g/cVDjQH
movabs rdi, 21474836480
call example::f1@PLT
vs mov edi, 5
pop rbp
jmp example::f2@PLT
so, yup.In other words, the compiler can turn an Option into a null-pointer convention by reasoning from first principles.
(I think this optimization works for enums in general, and is somehow related to the `nonzero::NonZero` type, but I could be wrong about that.)
Example (look at the calls to f1 and f2 in example::main): https://godbolt.org/g/SSs6X2
(I know nothing about std::variant yet.)
* it's platform-native on the major platforms and many others
* it's an international standard and has excellent backwards compatibility. i.e: it's not controlled by some corporation and it's very unlikely that a 2v3 Python-style fiasco would happen.
* it has a huge ecosystem. The only comparable ones are Java and maybe C#, but while there's overlap, each ecosystem has its focus. Java's is focused on the back-end and enterprise development.
* it's a flexible and powerful language
* it's basically the default option in certain domains
This comes pretty close to what you're asking for I believe.
I faced the similar problem. I invested some money in Effective Modern C++(Scott Mayer). It covers the language part. I borrowed a book for C++11 concurrency. But, modern C++ lacks high-quality material at one place like Golang/Rust(the language guys have very good documentation, not boring and good enough to learn quickly). There are tons of scattered example.