I’ve read in topics, articles and SO answers that the #define values have no type, I’ve made up my mind around this concept, thinking that the type is a property of the container variable, not a property of the value itself:
const char cVALUE = 100; // 'cVALUE' is char with value 100, wich type is '100'?
const short sVALUE = 100; // 'sVALUE' is short with value 100, wich type is '100'?
const int iVALUE = 100; // 'iVALUE' is int with value 100, wich type is '100'?
#define VALUE 100 // wich type is 'VALUE'?
But, what about the value suffixes?
#define VALUE_L 100l // 'VALUE_L' is long?
#define VALUE_UL 100ul // 'VALUE_UL' is unsigned long?
#define VALUE_LL 100ll // 'VALUE_LL' is long long?
#define VALUE_ULL 100ull // 'VALUE_ULL' is unsigned long long?
In the code above, the type seems attached to the value, so all this raw values are typed values, as opposed to what I’ve read before. But there’s more! text literals even have qualifiers, for example:
#define TEXT "Text" // '"Text"' is an array of some kind of chars.
The text value in the #define above has type (a character type, if you’re working with MSVC I think that the character type could vary changing the project settings -> Character set, no idea if it is possible in another IDEs) it also has the const cualifier and it is a LValue instead of an RValue, all this behaviour differences between numeric and text literals disturbs me.
So, assuming that the character type is char, the type of the literal "Text" is const char *, const char * const or const char[5]? or at least, it doesn’t have type at all before the correct type is deduced on the context?
And, in the C++11 standard, the text literals can also have type using some prefixes that sets the charset:
#define TEXT L"Text" // wide string with char type wchar_t
#define TEXTu8 u8"Text" // UTF-8 string with char type char
#define TEXTu u"Text" // UTF-16 string with char type char16_t
#define TEXTU U"Text" // UTF-32 string with char type char32_t
After thinking about all this stuff, I’m pretty confused, so I’m begging for some advice:
- Why is common knowledge that the literal values (and
#defines) have no type but a type can be specified with the literal? in other words: Assert that the value literals have no type is false? - A value literal without suffix and no decimals (like
100), can always be considered of type int? - Which is the type and qualifiers of the text literals, even considering its prefixes?
It’s not. Literals all have types, as specified in section 2.14 of the C++11 standard. Preprocessor macros are replaced before literals are interpreted.
No; a decimal literal is the first of
int,long intorlong long intthat can represent the value. Octal or hex literals may also be unsigned if necessary. Before 2011,long longwasn’t considered, since it wasn’t a standard type.So
100will have typeintsince it is small enough to be representable byint.With no prefix, it’s an array of
const char, large enough to hold all the characters and the zero terminator. So"Text"has typechar const[5].With prefixes, the character type changes to the types you give in the question; the array size is still large enough for all characters including the terminator.