Re: I want to use the g++ template in the "C" gcc software

Liste des GroupesRevenir à cl c  
Sujet : Re: I want to use the g++ template in the "C" gcc software
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.c
Date : 12. Dec 2024, 20:09:26
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20241212105336.312@kylheku.com>
References : 1
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2024-12-12, aotto1968 <aotto1968@t-online.de> wrote:
Hi,
>
I create a C software and there is a requirement to use the g++
template *in* the gcc.
I do *not* want to use all the g++ "boilerplate" like special syntax, classes etc
I just want to use the template.

It sounds like you want to work in a subset of C++ which includes the
entire C-like subset, plus template functions.

(Note that the class keyword in C++ is almost a synonym of struct.
In a dialect of C that has C++ templates, you should be able to justify
allowing yourself to use template structs, not only template functions.)

1. Is there a g++ switch to disable all the g++ features except of C source and template feature ?

g++ has few, if any, diagnostic options for excluding entire C++
features. You can turn off exception handling and RTTI.

It might also make sense to select an old dialect of C++, perhaps like
this:

  g++ -std=c++98 ...

(Unless you want to use newer features of C that appeared since C++98
and were added to C++.)

Obviously, since C++98 had classes, this will not turn off classes.

I myself maintain some code which is written in C++ that compiles as C
(or vice versa). But because you want to use templates, compiling as C
is out of the question.

When you stick to a strictly C compatible subset of C++ (no templates),
then ensuring you are not using special C++ features is very simple:
you regularly build the code with a C compiler!

Sometimes in my programming I have macros which are implemented
separately for C and C++. For instance for casting:

#ifdef __cplusplus
#define strip_qual(TYPE, EXPR) (const_cast<TYPE>(EXPR))
#define convert(TYPE, EXPR) (static_cast<TYPE>(EXPR))
#define coerce(TYPE, EXPR) (reinterpret_cast<TYPE>(EXPR))
#else
#define strip_qual(TYPE, EXPR) ((TYPE) (EXPR))
#define convert(TYPE, EXPR) ((TYPE) (EXPR))
#define coerce(TYPE, EXPR) ((TYPE) (EXPR))
#endif

For instance:

   const char *s0 = "abc";
   char *s1 = strip_qual(char *, s0);

In C, the strip_qual macro generates a C cast: (char *)(s0).
When compiled as C++, it becomes const_cast<char *>(s0).

Modern C has something called generic selection, which can do some
template-like things. *IF* your intended use of templates were simple
enough, it *might* be possible to wrap it behind some macros that
expand to C++ templates or to C generic selection.

Just an idea.

3. is there an external software to add the c++-template feature into
an existing C software ?

The first C++ compilers were developed by Bjarne Stroupstrup as a front
end called "cfront" which generated C. The source for that are
available, and date back to the nineties. I believe they have template
support. It's been quite a while since I looked at the code, but I seem
to remember template stuff in there. If so, it's probably behind even
C++98; would it do partial specialization and such? Don't know.

Anyway, using a forked and stripped down version of cfront, it may be
possible to create a "C With Templates" dialect of C. (C++ was
originally called C With Classes).

Then you wouldn't need g++ at all; cfront would translate the code
to plain C, compiled with gcc.


--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Date Sujet#  Auteur
12 Dec 24 * I want to use the g++ template in the "C" gcc software11aotto1968
12 Dec 24 +- Re: I want to use the g++ template in the "C" gcc software1Bonita Montero
12 Dec 24 +* Re: I want to use the g++ template in the "C" gcc software4David Brown
12 Dec 24 i`* Re: I want to use the g++ template in the "C" gcc software3aotto1968
12 Dec 24 i `* Re: I want to use the g++ template in the "C" gcc software2Chris M. Thomasson
12 Dec 24 i  `- Re: I want to use the g++ template in the "C" gcc software1Kaz Kylheku
12 Dec 24 +* Re: I want to use the g++ template in the "C" gcc software3Kaz Kylheku
12 Dec 24 i+- Re: I want to use the g++ template in the "C" gcc software1aotto1968
12 Dec 24 i`- Re: I want to use the g++ template in the "C" gcc software1Chris M. Thomasson
12 Dec 24 `* Re: I want to use the g++ template in the "C" gcc software2Chan
13 Dec 24  `- Re: I want to use the g++ template in the "C" gcc software1Keith Thompson

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal