Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the purpose of std::to_integer?

As far as I know, std::to_integer<T> is equivalent to T(value) where value is a variable having type std::byte.
I looked into some implementations from the major compilers and found that in this case equivalent means literally implemented as. In other terms, most of the times to_integer is actually implemented as:

return T(value);

And that's all.

What I don't understand is what's the purpose of such a function?
Ironically the cons are even more than the pros. I should include a whole header for such a function just to avoid a C-like cast that is most likely directly inlined anyway.

Is there any other reason for that or it's just really a nice looking alternative for a C-like cast and nothing more?

like image 997
skypjack Avatar asked Jun 22 '19 22:06

skypjack


People also ask

What is the point of std :: byte?

std::byte is a distinct type that implements the concept of byte as specified in the C++ language definition. Like char and unsigned char, it can be used to access raw memory occupied by other objects (object representation), but unlike those types, it is not a character type and is not an arithmetic type.

How do you initialize a byte STD?

std::byte is an enum class that was first introduced in C++17 and can be initialized using {} with any value from 0 to 255.

What is the byte in C++?

– Ed S. @Ben: The C and C++ standards unambiguously define a "byte" as the size of a char , which is at least 8 bits. The term "byte" may be defined differently in other contexts, but when discussing C or C++ it's better to stick to the standard's definition.


3 Answers

it's just really a nice looking alternative for a C-like cast and nothing more?

You say that as though it's some trivial detail.

Casts are dangerous. It's easy to cast something to the wrong type, and often compilers won't stop you from doing exactly that. Furthermore, because std::byte is not an integral type in C++, working with numerical byte values often requires a quantity of casting. Having a function that explicitly converts to integers makes for a safer user experience.

For example, float(some_byte) is perfectly legal, while to_integer<float>(some_byte) is explicitly forbidden. to_integer<T> requires that T is an integral type.

to_integer is a safer alternative.

I should include a whole header for such a function

If by "whole header", you mean the same header you got std::byte from and therefore is already by definition included...

like image 135
Nicol Bolas Avatar answered Nov 03 '22 09:11

Nicol Bolas


std::to_integer<T>(some_byte) is equivalent to T(some_byte) if it actually compiles. T(some_byte) is equivalent to the unsafe C-style cast of (T)some_byte, which can do scary things. On the other hand, std::to_integer is appropriately constrained to only work when it is safe:

This overload only participates in overload resolution if std::is_integral_v<IntegerType> is true.

If the T was not actually an integer type, rather than potentially having undefined behavior, the code won't compile. If the some_byte was not actually a std::byte, rather than potentially having undefined behavior, the code won't compile.

like image 42
Justin Avatar answered Nov 03 '22 11:11

Justin


Beyond the expression of intent and safety issues already mentioned, I get the idea from the committee discussion on the paper that it’s meant to be like std::to_string and might have more overloads in the future.

like image 4
Davis Herring Avatar answered Nov 03 '22 11:11

Davis Herring