Reputation: 31
Does this make any sense?
static_cast<long long>(1) == 1LL
static_cast<float>(1) =? 1F
Is there a short way of making the casting for other types such as float? Thank you very much!
Upvotes: 3
Views: 165
Reputation: 254501
This answer describes C++11. User-defined literals, and some of the types, didn't exist in historic versions of the language.
Integer literals can end with nothing, L
, LL
, U
, UL
or ULL
giving a type of int
, long
, long long
, unsigned int
, unsigned long
or unsigned long long
respectively. These can be in lower case if you like; and the actual type may be wider than specified if necessary to represent the value.
Floating-point literals can end with nothing, F
or L
giving a type of double
, float
or long double
respectively. Again, these can be in lower case if you like.
Character and string literals can begin with nothing, u
, U
or L
, giving a character type of char
, char16_t
, char32_t
or wchar_t
respectively. Strings can also begin with u8
to indicate a char
character type with UTF-8 encoding.
You can also define your own user-defined literals to make literals of any type, if you find weird things like 123_km
more readable than kilometres(123)
. I don't see the point of that, but someone's posted an example if you're interested.
Upvotes: 6
Reputation: 33671
Since C++11 you could define your own literals. For example, you could define literal _F
like this:
float operator"" _F(unsigned long long l)
{
return static_cast<float>(l);
}
int main()
{
auto a = 1_F;
static_assert(std::is_same<decltype(a), float>::value, "Not a float");
return 0;
}
Upvotes: 8