Charles W
Charles W

Reputation: 2292

Does C++ allow 8 byte long multi-character literals?

Why is this allowed:

uint32_t x = 'name';

But this gets truncated to 32 bits:

uint64_t x = 'namename';

Is there a way to have an 8-byte long multicharacter literal?

Upvotes: 3

Views: 492

Answers (2)

rcgldr
rcgldr

Reputation: 28826

If only 4 byte multi-character literats are supported, you could use:

uint64_t x = (((uint64_t)'abcd') << 32) + 'efgh';

but it would probably end up as 2 literals.

Upvotes: 0

Yes, as long as your compiler has 8-byte ints and supports it.

The C++ standard is farily terse regarding multi-character literals. This is all it has to say on the matter (C++14, 2.14.3/1):

An ordinary character literal that contains more than one c-char is a multicharacter literal. A multicharacter literal, or an ordinary character literal containing a single c-char not representable in the execution character set, is conditionally-supported, has type int, and has an implementation-defined value.

(Emphasis mine)

As you see, pretty much all the standard says is that if multicharacter literals are supported (they don't have to be), they are of type int. The value is up to the compiler.

Upvotes: 7

Related Questions