简体   繁体   中英

Is there a valid C++11 program with the expression 'C++11'?

The name of the programming language C++ derives from the parent language C and the ++ operator (it should arguably be ++C) and, hence, the expression C++ may naturally occur in C++ programs. I was wondering whether you can write a valid C++ program using the 2011 standard ( without extensions ) and containing the expression C++11 not within quotes and after pre-processing (note: edited the requirement, see also answer).

Obviously, if you could write a C++ program prior to the 2011 standard with the expressions C++98 or C++03 , then the answer is a trivial yes. But I don't think that was possible (though I don't really know). So, can it be done with the new armory of C++11?

NO if we require the characters C++11 to be outside any literal, after preprocessing -- because at translation phase 7 the three tokens will be identifier , ++ and integer-literal

The first two tokens are a postfix-expression , the later is a primary .

There is no reduction in the grammar that can contain these two nonterminals in sequence, so any program containing C++11 will fail syntax analysis.

However , if you do not consider character literals to be strings, then the answer is YES as you can contain it in a wide character literal:

int main()
{
    wchar_t x = L'C++11';
}

which does not use the preprocessor or a string literal, and the construct is required to be supported by the standard:

The value of a wide-character literal containing multiple c-chars is implementation-defined.

So, can it be done with the new armory of C++11?

No.

Define “valid C++ program”.

The C++ standard defines a “well-formed C++ program” as “a C++ program constructed according to the syntax rules, diagnosable semantic rules, and the One Definition Rule”. This leaves open the possibility of C++ programs that are not well-formed. (C explicitly has the notion of programs that are conforming but not strictly conforming, eg, that use extensions of a particular compiler.)

If you consider it valid to use extensions, then you can implement a C++ compiler that permits C++11 in some context.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM