2

A such language could otherwise be constructed by interpreting the source as C if it's not valid C++ and interpret it as C++ if it's not valid C and interpret it as C/C++ if it's valid for both. This requires that code that are both valid C and C++ could conformingly be interpreted the same (undefined behaviour in one language and defined behavior in the other could fx be interpreted the same).

I know there's at least one construct that is valid in both and not interpreted the same. The preprocessor has predefined macros that differ in C and C++, so for example:

int fubar(void)
{
#ifdef __cplusplus
    return 1;
#else
    return 0;
#endif
}

which is valid for both, but have different meaning.

skyking
  • 13,817
  • 1
  • 35
  • 57
  • 5
    http://stackoverflow.com/questions/12887700/can-code-that-is-valid-in-both-c-and-c-produce-different-behavior-when-compile?rq=1 – Mat Aug 21 '15 at 07:12
  • The `?:` operator does different things when type conversion is involved, but I cannot find an example. – nwp Aug 21 '15 at 07:19
  • While there are examples in the other question that answers this, there are some that are not valid, fx `sizeof('a')` **could** evaluate to `1` in both C and C++. – skyking Aug 21 '15 at 07:28
  • Your criteria is "valid for both, but have different meaning" ; in C, `sizeof('a')` is semantically `sizeof(int)`, but it is `sizeof(char)` in C++. That's a different meaning. Whether some platforms would evaluate both to the same value isn't really relevant. – Mat Aug 21 '15 at 09:08
  • @Mat It's kind of relevant. I pointed out that it would require that they *could* conformingly be interpreted the same. The "different meaning" boils down to yielding the same result - the `sizeof('a') could evaluate to `1` in both. – skyking Aug 21 '15 at 09:18
  • That doesn't make sense in the context of your question, unless you fix (or try to define) a specific implementation of C and C++ for it. Sure a C implementation could exist where sizeof(int) is 1. That doesn't "help" making C++ a superset of C in general, just for that specific implementation. – Mat Aug 21 '15 at 10:10
  • @Mat I was not refering to `C++`, but a hypotetical extension of it. – skyking Aug 21 '15 at 10:26

0 Answers0