C4455: ‘operator “”w’: literal suffix identifiers that do not start with an underscore are reserved

By , last updated January 18, 2015

User defined literals are a new feature in C++ since C++11. In it’s simplest form, it allows the programmer to use a suffix on any literal in the code. The most obvious example is to use user defined literals to strongly convert time and make it possible to add weeks, days and seconds without using cumbersome libraries like boost::units and similar.

But you have to remember to stick to the rules. In Visual Studio (2015 preview) you’ll get C4455 if you try to define an user defined literal without a leading underscore. That is very good. However, that’s the only violation Visual Studio will report (at least with 2015 preview).

// Not OK. C4455
long double operator "" w(long double value)
{
	return value;
}

// OK
long double operator "" _w(long double value)
{
	return value;
}

// Not OK. Underscore and an upper case letter are reserved.
long double operator "" _E(long double value)
{
	return value;
}

// Not OK. Double underscores are reserved.
long double operator "" __w(long double value)
{
	return value;
}

If you include , you can use some of those defined related to time (since C++14).

auto seconds = 1.2s + 2h + 6min;

// seconds = 7561.5000000000000 seconds

Pretty neat.

Comments

Be the first to comment.

Leave a Reply


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*