C is not a subset of C++?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Oliver Dain

    C is not a subset of C++?

    I've seen it asserted in a few places that C is not a proper subset of
    C++, but I don't understand the assertion. What things are valid C
    that aren't valid C++?

    Thanks.
  • Oliver S.

    #2
    Re: C is not a subset of C++?

    > I've seen it asserted in a few places that C is not a proper[color=blue]
    > subset of C++, but I don't understand the assertion.[/color]

    Read this: http://www.research.att.com/~bs/3rd_compat.pdf

    Comment

    • Oliver Dain

      #3
      Re: C is not a subset of C++?

      Oliver S. wrote:
      [color=blue][color=green]
      >> I've seen it asserted in a few places that C is not a proper
      >> subset of C++, but I don't understand the assertion.[/color]
      >
      > Read this: http://www.research.att.com/~bs/3rd_compat.pdf[/color]

      Thanks much. That was a great reference.

      Comment

      • Dave Vandervies

        #4
        Re: C is not a subset of C++?

        In article <eZO4b.5032$tw6 .3616@newsread4 .news.pas.earth link.net>,
        Oliver Dain <odain2@nospam. mindspring.com> wrote:[color=blue]
        >I've seen it asserted in a few places that C is not a proper subset of
        >C++, but I don't understand the assertion. What things are valid C
        >that aren't valid C++?[/color]

        As an example, this will compile and run (but do nothing interesting)
        as C, but not as C++:
        --------
        /*'.h' form of C headers is bad form in C++ - use <cstdlib> instead*/
        #include <stdlib.h>

        /*Implicit int - Valid (but deprecated) C, not valid C++*/
        main()
        {
        /*'new' as variable name - Valid C, not valid C++*/
        int *new;

        /*Implicit conversion from void * - Valid C, not valid C++*/
        new=malloc(42*s izeof *new);

        /*Implicit conversion *to* void * is valid in both languages*/
        free(new);

        /*Casting malloc() - bad code in both languages
        (Hides bugs like failing to #include <stdlib.h> in C,
        new should be used instead of malloc in C++)
        */
        /*Character constants with type int - valid C, silent bug in C++
        (unless sizeof(int)==si zeof(char))
        */
        new=malloc(17*s izeof 'a');

        free(new);

        /*Falling off the end of main() - different semantics depending on language
        C90 returns unspecified exit status, C99 and C++ treat it like 'return 0'
        */
        }
        --------

        Plus there are some more esoteric differences like different handling
        of '//' comments that are unlikely to show up in code not deliberately
        written to demonstrate them.

        Google for 'subset' in comp.lang.c for more than you ever wanted to know
        about this.


        Note that if you're willing to get a bit more hand-wavey, you can
        legitimately claim that the expressive power of C is a subset of the
        expressive power of C++ (though this is less true with C99 than with C90);
        a C program can be made into a valid C++ program with the same semantics
        without any major structural changes (though there will probably be a
        bunch of code-level changes that are needed).


        dave

        --
        Dave Vandervies dj3vande@csclub .uwaterloo.ca
        [Y]ou can write bad code that just barely works in both languages, or good
        code that works only in one language -- so pick one, and write good code in
        that language. --Chris Torek in comp.lang.c (crossposted to comp.lang.c++)

        Comment

        • Russell Hanneken

          #5
          Re: C is not a subset of C++?

          "Oliver Dain" <odain2@nospam. mindspring.com> wrote in message
          news:eZO4b.5032 $tw6.3616@newsr ead4.news.pas.e arthlink.net...[color=blue]
          > I've seen it asserted in a few places that C is not a proper subset of
          > C++, but I don't understand the assertion. What things are valid C
          > that aren't valid C++?[/color]

          There's a web page devoted to answering that exact question:



          I've found it to be an excellent reference.

          Regards,

          Russell Hanneken
          rhanneken@pobox .com



          Comment

          • Default User

            #6
            Re: C is not a subset of C++?

            Dave Vandervies wrote:
            [color=blue]
            > /*Implicit int - Valid (but deprecated) C, not valid C++*/
            > main()[/color]

            Actually NOT valid C according to the latest standard. You should make
            that clear here as you did in later points.



            Brian Rodenborn

            Comment

            Working...