DEV Community

Discussion on: Understanding Rust as a C++ developer

Collapse
 
thedenisnikulin profile image
Denis • Edited

Hi, great article, thank you!
By the way, I'm not a C++ developer, but as far as I'm concerned, the common primitive types like int in C++ are machine-dependent, e.g. in this line:

// int i = -8
let i = -8i32;
Enter fullscreen mode Exit fullscreen mode

wouldn't it be better to write fixed-width types for clarity like this instead

// int32_t i = -8;
let i = -8i32;
Enter fullscreen mode Exit fullscreen mode

Thanks.

Collapse
 
daaitch profile image
Philipp Renoth

Hey @thedenisnikulin,

fun fact: I also got this wrong in my first version and thought that int is 64bit on 64bit-arch. What I found out was, that int can be 64bit, but most of the mainstream compilers will use 32bit for some reasons you can find for example in this thread here: stackoverflow.com/questions/174898...

It's really a mess in C++ :D, but you can check it out on your machine:

    cout
        << "int: " << sizeof(int)
        << ", void*: " << sizeof(void*)
        << ", long: " << sizeof(long)
    ;
Enter fullscreen mode Exit fullscreen mode

So int is "mostly good enough" for counting or accessing "array-like" types, but of course not for storing pointers or the difference of pointers.