16

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?

20
  • 5
    Written alone, C++ grammar would always parse C++11 as ((C)++)11) (ordering, not literal parenthesis). And given any symbol x, x11 doesn't seem to be parseable. Apr 4, 2013 at 16:14
  • 11
    Hm, I'm not convinced this question is off topic. It's specific, it's unique to the programming profession, it's answerable, and although not visibly practical, it's a "fun" question whose answer may contain interesting or insightful information about syntax. At least, I've seen other "fun" questions that haven't been closed as being off topic. Apr 4, 2013 at 16:28
  • 5
    c + + 11 is valid (as is c+ +11 of course)
    – hexist
    Apr 4, 2013 at 16:44
  • 5
    If macros were allowed this would be easy: #define C (newline) C++11[(char*)0];
    – Aaron
    Apr 4, 2013 at 16:59
  • 5
    #include <C++11.hpp> is not a string actually (h-char-sequence).
    – dyp
    Apr 4, 2013 at 18:32

3 Answers 3

8

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.

6
  • hmmm. the answer has changed since I accepted it. I don't like that (you could have written another one). The trem "string" in my question is ambiguous: it could be narrowly interpreted as std::string, but I would regard anything within quotes (single or double) as "string".
    – Walter
    Apr 6, 2013 at 8:34
  • rather than unaccepting your answer, I edited it. I hope that's okay for you. thanks for you effort!
    – Walter
    Apr 6, 2013 at 8:45
  • 1
    It is wildly inappropriate to edit someones answer to completely change the semantic content, especially when you don't know the difference between a character literal and a string literal. Apr 6, 2013 at 13:06
  • sorry, no harm intended. I object, though, to your completely change the semantic content -- I don't think I did that, so my edit was not wildly inappropriate, I merely tried to adapt the answer to remain an acceptable one.
    – Walter
    Apr 6, 2013 at 17:34
  • The reasoning doesn't take into account that n++11 for example can occur inside a valid program (if n belongs to return).
    – ipc
    Apr 6, 2013 at 18:25
0

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

No.

2
  • 2
    hmm. I could just believe you, but I rather accept and answer that comes with some good arguments.
    – Walter
    Apr 5, 2013 at 23:50
  • 1
    @Walter: I can't prove a negative to you, unless you want me to quote the entire C++11 standard. All the "good arguments" in other answers apply to previous C++ standards too, and are therefore not part of answering your question "has this changed in C++11?" Apr 6, 2013 at 14:02
0

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, e.g., 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.

1
  • +1 for the idea. But extensions are, obviously, not allowed. They alter the language. With extensions almost anything can be rendered valid.
    – Walter
    Apr 6, 2013 at 10:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.