Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any C++ style guide that talks about numeric literal suffixes?

In all of the C++ style guides I have read, I never have seen any information about numerical literal suffixes (i.e. 3.14f, 0L, etc.).

Questions

  1. Is there any style guide out there that talks about there usage, or is there a general convention?

  2. I occasionally encounter the f suffix in graphics programming. Is there any trend on there usage in the type of programming domain?

like image 731
Jesse Good Avatar asked May 04 '12 23:05

Jesse Good


Video Answer


2 Answers

There is no general style guide that I've found. I use capital letters and I'm picky about using F for float literals and L for long double. I also use the appropriate suffixes for integral literals.

I assume you know what these suffixes mean: 3.14F is a float literal, 12.345 is a double literal, 6.6666L is a long double literal.

For integers: U is unsigned, L is long, LL is long long. Order between U and the Ls doesn't matter but I always put UL because I declare such variables unsigned long for example.

If you assign a variable of one type a literal of another type, or supply a numeric literal of one type for function argument of another type a cast must happen. Using the proper suffix avoids this and is useful along the same lines as static_cast is useful for calling out casts. Consistent usage of numeric literal suffixes is good style and avoids numeric surprises.

People differ on whether lower or upper case is best. Pick a style that looks good to you and be consistent.

like image 90
emsr Avatar answered Oct 19 '22 14:10

emsr


The only established convention (somewhat established, anyway) of which I'm aware is to always use L rather than l, to avoid its being mistaken for a 1. Beyond that, it's pretty much a matter of using what you need when you need it.

Also note that C++ 11 allows user-defined literals with user-defined suffixes.

like image 35
Jerry Coffin Avatar answered Oct 19 '22 15:10

Jerry Coffin