Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > C is not a subset of C++?

Reply
Thread Tools

C is not a subset of C++?

 
 
Oliver Dain
Guest
Posts: n/a
 
      09-01-2003
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.
 
Reply With Quote
 
 
 
 
Oliver S.
Guest
Posts: n/a
 
      09-01-2003
> 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.


Read this: http://www.research.att.com/~bs/3rd_compat.pdf
 
Reply With Quote
 
 
 
 
Oliver Dain
Guest
Posts: n/a
 
      09-01-2003
Oliver S. wrote:

>> 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.

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


Thanks much. That was a great reference.
 
Reply With Quote
 
Dave Vandervies
Guest
Posts: n/a
 
      09-02-2003
In article <eZO4b.5032$(E-Mail Removed). net>,
Oliver Dain <(E-Mail Removed)> wrote:
>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++?


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*sizeof *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)==sizeof(char))
*/
new=malloc(17*sizeof '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 http://www.velocityreviews.com/forums/(E-Mail Removed)
[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++)
 
Reply With Quote
 
Russell Hanneken
Guest
Posts: n/a
 
      09-03-2003
"Oliver Dain" <(E-Mail Removed)> wrote in message
news:eZO4b.5032$(E-Mail Removed) ink.net...
> 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++?


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

http://david.tribble.com/text/cdiffs.htm

I've found it to be an excellent reference.

Regards,

Russell Hanneken
(E-Mail Removed)



 
Reply With Quote
 
Default User
Guest
Posts: n/a
 
      09-03-2003
Dave Vandervies wrote:

> /*Implicit int - Valid (but deprecated) C, not valid C++*/
> main()


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



Brian Rodenborn
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Are sequence containers not a subset of general containers? Sebastian Mach C++ 5 10-06-2012 07:54 PM
Deploying a subset of an asp.Net app =?Utf-8?B?S2V2aW4gQmFua3M=?= ASP .Net 0 10-21-2004 12:55 PM
validators subset news.microsoft.com ASP .Net 1 05-27-2004 10:28 PM
Datagrid - populate with subset of an XML file Andy ASP .Net 0 05-24-2004 03:53 PM
subset of data using dataview?? Guoqi Zheng ASP .Net 2 01-19-2004 01:54 PM



Advertisments