Is there an library for C compiler which will add C++'s features?
Printable View
Is there an library for C compiler which will add C++'s features?
Not really, since C++ is a different language. Some compilers (and the C99 standard) has cherry picked a few things that they find handy out of the C++ language (variable sized arrays and "variable declarations anywhere" being the most obvious ones).
On the one hand, you could say that all C++ features can actualy be achieved in C. On the other hand, C++ _IS_ a different language, and to add those features to C would require changing the compiler - you can't just use a library as replacement.
But bear in mind that the first C++ compiler actually produced C code, and used a standard C compiler to generate machine code.
--
Mats
Is there a reason you can't use C++ instead?
I don't know of such a library, but if you need to implement an object orientated design in C, then there are different strategies for this. Some links that might be useful:
ldeniau.web.cern.ch/ldeniau/html/oopc/oopc.html
www.planetpdf.com/codecuts/pdfs/ooc.pdf
www.bolthole.com/OO-C-programming.html
>>Not really, since C++ is a different language. Some compilers (and the C99 standard) has cherry picked a few things that they find handy out of the C++ language (variable sized arrays and "variable declarations anywhere" being the most obvious ones).
When did C++ get variable sized arrays?
You can do that in C99, so it's not really a C++ feature :)
But since when did C++ allow arrays with a non-compile time constant arrays?
Your example doesn't compile, mats!
Ooops my bad ;). Sorry matsp.
Sorry, C++ doesn't technically support that, but gcc does as an extension - I got myself confused [again]. I'm not sure if C99 does support it or not - gcc -std=c99 -pedantic does compile this:
whilst g++ -std=c++98 -pedantic does not. Remove pedantic and it does.Code:#include <stdio.h>
double func(const int n)
{
int arr[n];
double d = 0;
for(int i = 0; i < n; i++)
{
arr[i] = 1 << i;
}
for(int j = 0; j < n; j++)
{
d += 1.0 / arr[j];
}
return d;
}
int main()
{
int n;
printf("Enter number of elements:");
scanf("%d", &n);
printf("Result=%f\n", func(n));
return 0;
}
--
Mats
VLAs were a GCC extension and were adopted in C99. They're not part of any C++ standard, and won't be part of C++0x, AFAIK.
GCC's documentation says, however, that their implementation of VLAs is not 100% compliant with the C99 definition.
http://gcc.gnu.org/c99status.html
A good library for getting object-oriented functionality into C is glib/gobject.
Yes, that's the whole point asking here. :)
A library I am using compiles only with a C compiler, but not with a C++ compiler. My platform and compiler is outdated. Also no C++ compiler available with full (compared to recently compilers) standard support.
C++ as "addon" for a standard C compiler would have been a nice thing, but if it doesn't exists I am out of luck.
That would be quite impossible. Different syntax means the compiler itself must change.
I'm not sure if I understand your problem well, but you can mix C and C++ code. In short you compile your C code with the C compiler and C++ code with the C++ compiler. By supplying linkage specifications you're able to use C code within C++ code.