Skip to main content

Put those literals into your code

Since we have seen C++11 we obtained an ability to create user defined literals both prefix and suffix forms.
But let's start from the begging, shall we?



So what is literals in the first place? It's a special characters that allows a compiler to know the exact type of a value that you're initialize it with.

So you must have seen them before

0xDEADBEEF
3.14f
429496729u
0010

Those constructions allows you to define type of values to avoid unnecessary copy. Usually it isn't such a big deal but it allows you to write more meaningful code and avoid ambiguity. Wouldn't it be nice to see a expressive and obvious constant instead of just a number that used in N places in a function?

Currently there is a way to create you own useful literals. Those functions can make your code a bit more clean. Using them doesn't solve a problem with magic numbers but makes them more self-explanatory.

But let's get back to the topic.

#include <iostream>
#include <string>
//#include <string_view>
constexpr long operator"" _KB(unsigned long long int value) { return value * 1024; }
long operator"" _MB(unsigned long long int value) { return value * 1024 * 1024; }
long operator"" _GB(unsigned long long int value) { return value * 1024 * 1024 * 1024; }
class A
{
public:
A(std::string str) { std::cout << "A(string) called\n"; }
//A(std::string_view str) { std::cout << "A(string_view) called\n"; }
A(const char* str) { std::cout << "A(const char*) called\n"; }
};
int main(int argc, char** argv)
{
//using namespace std::literals;
using namespace std::string_literals;
std::cout << "5 KB is " << 5_KB << " 7 MB is " << 7_MB << " 2 GB is " << 2_GB << "\n";
A a("whats up\n");
A b("whats up\n"s);
//A c("whats up\n"sv);
return 0;
}
view raw thoseliterals hosted with ❤ by GitHub
In the example you can see few literals I've defined to show you some use cases. Those are simple ones but you can make them as complex as you wish. But you need to remember about complexity of those literals. Don't put a for loop over 100.000 elements in it. No really, don't.

Literals have some limitations on defining them. You wont be able to create all possible variations of parameter list. There is more info about it on cppreference.

For me literals is just another way to structure the code in a whole neat package that makes sense for people that see my work. That's the point.

Popular posts from this blog

Constness of an object

Defining a function you might want to make some of it's arguments to be constant to avoid unnecessary copying or have a bit more strict interface. There is another way to use constness - there we meet constant class members.

Move semantics

For a long time in C++ was no fast way to deal with return value. They were copied and there was no hope to change it. Some have used return parameters in argument list like: void GetObjectsInArea(Area exactArea, ObjectList& objList); To avoid unnecessary copies and speed up the execution developers were doing a lot of work. Eventually when optimized code were becoming unreadable the developers would abandon it and the code would transform into legacy code at some point.

Templates and how to fold them

Variadic templates appeared in C++11 to cover such cases when you would have a template functions that could have a numerous members of different types. Doesn't it remind you of variadic functions that uses va_start, va_arg, va_end and so others? Cause it should be.