Abbasi
But how they're faster than std::map? By the way how are the key kept unique?
Vlad
No, C casts can cast away and cast in const and volatile
It's a problem only for references, no?
Vlad
https://godbolt.org/z/GE6E6n7ro
Ludovic 'Archivist'
Your suggestion is to use arrays instead of the std::map?
Yes, std::map is overkill, it is pretty expensive to build, and pretty expensive to look up it. std::map will evict log(n) cache lines when doing your lookup for the value, as well as disrupt the branch predictor, while using an array will literally compile the lookup to one instruction
Vlad
https://godbolt.org/z/GE6E6n7ro
Both c cast and static_cast here remove cv for values
Ludovic 'Archivist'
It's a problem only for references, no?
Yes, I just want them to take good habits in case they work with templates in the future
Ludovic 'Archivist'
Vlad
Yes, I just want them to take good habits in case they work with templates in the future
I see, I just find them to be too verbose for usercode value casting tbh
Vlad
Nope, here it does a copy
Where? Asm is identical
Ludovic 'Archivist'
Where? Asm is identical
of course it is, here it does a copy in both case
Ludovic 'Archivist'
Where? Asm is identical
https://godbolt.org/z/YrxnTvbd9
Vlad
Yeah, I'm talking about values though, not references. For references C casts are deadly yeah
Vlad
Not denying that
Ludovic 'Archivist'
Yeah, I'm talking about values though, not references. For references C casts are deadly yeah
Yes, it is a question of not getting bad habits. Basically code hygiene. I write a lot of templates and a wrongly placed C-cast can take quite a few week to debug
Ludovic 'Archivist'
Thankfully I do not contribute to your code base
What a coincidence, I was thinking the same
Pavel
Thankfully I do not contribute to your code base
Is there any benefit of using c-cast apart from it being shorter?
Pavel
Readability is controversial. I feel like c-casts are easier to hide in other code
Pavel
I think in about all projects I worked on c-casts were banned
Pavel
And reinterpret_cast as well
Vlad
seeing code related makes me nauseous a little bit int a = ...; int b = ...; foobar(static_cast<size_t>(a), static_cast<short>(b));
Vlad
Cant help it
Pavel
seeing code related makes me nauseous a little bit int a = ...; int b = ...; foobar(static_cast<size_t>(a), static_cast<short>(b));
The code is clearly showing that there is a possibility to lose some data here, which I think is good
Vlad
But it aint 40 symbols
Ludovic 'Archivist'
So does the C cast
How do you look up C-casts in your code base?
Vlad
How do you look up C-casts in your code base?
If you have a specific type in mind you just literally ctrl+f/grep it. Again why would you look up c casts for values?
Vlad
I get why would you grep reinterpret casts or const casts
Ludovic 'Archivist'
If you have a specific type in mind you just literally ctrl+f/grep it. Again why would you look up c casts for values?
How do you know if your C cast applies to a value for sure if you are in a templated function/class
Cayetano
Guys, I guess that you are mixing C and C++. I'm convinced that C++ is enough optimized to use STL and resolve the issues that you are mention... isn't it?
Vlad
With like a typename
Vlad
Obviously
Vlad
Obviously
But static casting ints is just pointless
Cayetano
Are you referring to the std::map conversation?
I'm referencing that in C++ you can optimize the code in same way what we did with C
Vlad
Vlad
And then putting std::vector's into std::map
Cayetano
Well, the only way that I find to solve my situation is that, because, if I implement the solution suggested by @Abbasi_dev, that is (following message)
Cayetano
. . . enum class languages { es_AR,es_BO,es_CL,es_CO,es_CR,es_DO,es_EC,es_ES,es_GT, ... }; const std::map<languages, const char *> languagesNames{ {languages::es_AR, "Spanish (Argentina)"}, {languages::es_BO, "Spanish (Bolivia)"}, {languages::es_CL, "Spanish (Chile)"}, .... }; ```
Cayetano
I get from system a string like "es_ES" or "en_US", and I need to find the match in the enum... How can I do that?
Vlad
Usually localization is done via resource files that then get embedded into the executable
And all of the stuff above it does itself, you only supply it with your locale and string_id of your phrase
Abbasi
Well, the only way that I find to solve my situation is that, because, if I implement the solution suggested by @Abbasi_dev, that is (following message)
Whether an array is faster than a std::map depends on many factors, especially for such small maps. Also, using the map you can have the keys unique, as mentioned.
Cayetano
Even if you choose the path to store in a file and load it dinamically... That part I guess that I have clear...
Ludovic 'Archivist'
I get from system a string like "es_ES" or "en_US", and I need to find the match in the enum... How can I do that?
with an extra array for those mappings, using the property of the iso language strings to compress the 5 byte string into a uint32_t and mapping that to the enums
Cayetano
My issue now is, you get from system a string (char*) and you need to find out how to match it to the enum or std::map structures...
Cayetano
But the enum part is what I cannot see clear
Abbasi
You can always measure.
Vlad
Which will be totally fine since not like he's gonna have more than 10 languages
Cayetano
with an extra array for those mappings, using the property of the iso language strings to compress the 5 byte string into a uint32_t and mapping that to the enums
So, are you saying to have a map, an array and a enum to keep the same that I have with only one map and a bunch of #defines? 🤔
Ludovic 'Archivist'
why would you use a map
Ludovic 'Archivist'
No defines either
Cayetano
Just a std::map<string, string> to store pairs like {'en_US', 'American English'}?
Ludovic 'Archivist'
just std::array, a reinterpret_cast and enum to int convertions
Ludovic 'Archivist'
Just a std::map<string, string> to store pairs like {'en_US', 'American English'}?
Stop using std::map at all, it is literally deleting your braincells
Cayetano
Sorry I'm a little bit newbie, how can I use a reinterpret_cast and enum? 🤔 Can you show me briefly, please 🙏?
Cayetano
@cayetanohosma I did already
Oh! sorry, let's me see...
Ludovic 'Archivist'
as for the reinterpret cast, look at those language strings and find the character that is so redundant you could remove it
Vlad
So, are you saying to have a map, an array and a enum to keep the same that I have with only one map and a bunch of #defines? 🤔
struct Lang { uint32_t code; char description[28]; }; Lang langs[147]; auto lang = std::find_if(std::begin(langs), std::end(langs), [](auto& l){ return l.code == 0xB00B5: });
Vlad
Although I would go for char[6] instead
Ludovic 'Archivist'
Although I would go for char[6] instead
At least use a char[8], no need to put weird alignment in here
Vlad
At least use a char[8], no need to put weird alignment in here
It's a struct anyway you can add those 2 bytes to the next member
Vlad
Yeah load store would take 2 instructions rather than 1, don't care really
Vlad
It's a language select not a video decoder
Cayetano
hmmm... That's true!
Cayetano
Ok, let me to make some proofs and I'll be back with the final solution... Thank you very much guys!
Orange Juice
Welcome!
Oliver
I have completed MCA course
Oliver
Please give me suggestion to choose the technical field