Discussion:
I want to use the g++ template in the "C" gcc software
Add Reply
aotto1968
2024-12-12 08:37:56 UTC
Reply
Permalink
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.

goal:

1. I already use "inline" code to replace "cpp-macro-like" syntax in the header.
2. NOW I *want* to replace a "cpp-macro" which use the *data-type* as an argument with a template.

question.

1. Is there a g++ switch to disable all the g++ features except of C source and template feature ?
2. Is there a gcc switch to add g++-template feature into gcc ?
3. is there an external software to add the c++-template feature into an existing C software ?

thanks.
Bonita Montero
2024-12-12 10:05:34 UTC
Reply
Permalink
Post by aotto1968
Hi,
I create a C software and there is a requirement to use the g++ template *in* the gcc.
Then wrap your templated code inside extern "C" thunk functions.
David Brown
2024-12-12 10:24:04 UTC
Reply
Permalink
Post by aotto1968
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.
1. I already use "inline" code to replace "cpp-macro-like" syntax in the header.
2. NOW I *want* to replace a "cpp-macro" which use the *data-type* as an
argument with a template.
question.
1. Is there a g++ switch to disable all the g++ features except of C
source and template feature ?
2. Is there a gcc switch to add g++-template feature into gcc ?
3. is there an external software to add the c++-template feature into an
existing C software ?
thanks.
It might help if you give an example of what you are trying to achieve -
it doesn't have to be valid code that can be compiled.

gcc has command-line options to disable some features of C and C++, but
not like you are describing. And C++ without classes is not the same as
C anyway. But you might find you can write your code in C++ and simply
not use the features you don't want to use.

If you are trying to make type-generic function-like "things" in C, then
you have to use macros. But you can use modern features of C11 and C23
to make this safer and neater than in older C standards. gcc also
provides extensions that can help, if you are happy writing gcc-specific
code.

You should do your own googling for information, tutorials and examples
here, but these links will give you some starting points:

C11 _Generic selections:
<https://en.cppreference.com/w/c/language/generic>

C23 "typeof" and "auto" type inference:
<https://en.cppreference.com/w/c/language/typeof>
<https://en.cppreference.com/w/c/language/auto>

gcc extensions:
<https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html>
<https://gcc.gnu.org/onlinedocs/gcc/Typeof.html>
aotto1968
2024-12-12 15:17:19 UTC
Reply
Permalink
Post by aotto1968
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.
1. I already use "inline" code to replace "cpp-macro-like" syntax in the header.
2. NOW I *want* to replace a "cpp-macro" which use the *data-type* as an argument with a template.
question.
1. Is there a g++ switch to disable all the g++ features except of C source and template feature ?
2. Is there a gcc switch to add g++-template feature into gcc ?
3. is there an external software to add the c++-template feature into an existing C software ?
thanks.
It might help if you give an example of what you are trying to achieve - it doesn't have to be valid code that can be compiled.
gcc has command-line options to disable some features of C and C++, but not like you are describing.  And C++ without classes is
not the same as C anyway.  But you might find you can write your code in C++ and simply not use the features you don't want to use.
If you are trying to make type-generic function-like "things" in C, then you have to use macros.  But you can use modern
features of C11 and C23 to make this safer and neater than in older C standards.  gcc also provides extensions that can help, if
you are happy writing gcc-specific code.
<https://en.cppreference.com/w/c/language/generic>
<https://en.cppreference.com/w/c/language/typeof>
<https://en.cppreference.com/w/c/language/auto>
<https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html>
<https://gcc.gnu.org/onlinedocs/gcc/Typeof.html>
Thanks for your links but I already know "feature" of gcc etc…

I still want to get better "macro" code use template like c++ has

example: (the 'cls' is a type and the `pBufferInit_T` create a type specific "inline"
-> this I want to replace with an template.

#define pBufferInit_T(pre, cls) \
mk_inline void \
p##cls##Init ( \
MK_RT_ARGS \
pre##cls##C##R * const loc, \
MK_TYP const type, \
MK_NUM const size \
) { \
pppBufferInit(MK_RT_CALL pre##cls##C##_X2buf(loc),size, \
(MK_NUM)((*type).objsize-sizeof(pre##cls##C##R)), \
offsetof(pre##cls##C##R,ils_data)); \
if (type->objsig != (*pre##cls##C##_X2obj(loc)).signature) { \
(*pre##cls##C##_X2obj(loc)) = MkObjInitFromType(type,true /* isLocal */); \
} \
/* ppBufferInit */ \
/* ppBufferStreamInit */ \
pp##cls##Init(MK_RT_CALL loc); \
/* pBufferReset */ \
/* pBufferStreamReset */ \
p##cls##Reset(loc); \
} \
Chris M. Thomasson
2024-12-12 21:58:22 UTC
Reply
Permalink
Post by aotto1968
Post by David Brown
Post by aotto1968
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.
1. I already use "inline" code to replace "cpp-macro-like" syntax in the header.
2. NOW I *want* to replace a "cpp-macro" which use the *data-type* as
an argument with a template.
question.
1. Is there a g++ switch to disable all the g++ features except of C
source and template feature ?
2. Is there a gcc switch to add g++-template feature into gcc ?
3. is there an external software to add the c++-template feature into
an existing C software ?
thanks.
It might help if you give an example of what you are trying to achieve
- it doesn't have to be valid code that can be compiled.
gcc has command-line options to disable some features of C and C++,
but not like you are describing.  And C++ without classes is not the
same as C anyway.  But you might find you can write your code in C++
and simply not use the features you don't want to use.
If you are trying to make type-generic function-like "things" in C,
then you have to use macros.  But you can use modern features of C11
and C23 to make this safer and neater than in older C standards.  gcc
also provides extensions that can help, if you are happy writing gcc-
specific code.
You should do your own googling for information, tutorials and
<https://en.cppreference.com/w/c/language/generic>
<https://en.cppreference.com/w/c/language/typeof>
<https://en.cppreference.com/w/c/language/auto>
<https://gcc.gnu.org/onlinedocs/gcc/Statement-Exprs.html>
<https://gcc.gnu.org/onlinedocs/gcc/Typeof.html>
Thanks for your links but I already know "feature" of gcc etc…
I still want to get better "macro" code use template like c++ has
example: (the 'cls' is a type and the `pBufferInit_T` create a type specific "inline"
-> this I want to replace with an template.[...]
ot, but some fun with macros:

https://github.com/rofl0r/chaos-pp

;^)
Kaz Kylheku
2024-12-12 22:10:54 UTC
Reply
Permalink
Post by Chris M. Thomasson
https://github.com/rofl0r/chaos-pp
When the C preprocessor gets applied to a language with a lighter
syntax without declaations, things really start happening.

https://kylheku.com/cgit/cppawk/about

cppawk ships with an iteration macro that takes multiple clauses that
are taken in parallel or cross-product wise. Moreover, the vocabulary
of loop clauses can be extended by the program, by writing a few simple
macros (per clause).
--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @***@mstdn.ca
Kaz Kylheku
2024-12-12 19:09:26 UTC
Reply
Permalink
Post by aotto1968
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.)
Post by aotto1968
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.
Post by aotto1968
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: @***@mstdn.ca
aotto1968
2024-12-12 20:44:15 UTC
Reply
Permalink
Post by Kaz Kylheku
Post by aotto1968
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.)
Post by aotto1968
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
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),
you regularly build the code with a C compiler!
Sometimes in my programming I have macros which are implemented
#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
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.
Post by aotto1968
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.
thanks, good summary.
Chris M. Thomasson
2024-12-12 22:00:13 UTC
Reply
Permalink
Post by Kaz Kylheku
Post by aotto1968
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.)
For the op: struct in C++ makes everything public by default. A class
makes everything private by default.
[...]
Chan
2024-12-12 22:17:50 UTC
Reply
Permalink
Post by aotto1968
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.
1. I already use "inline" code to replace "cpp-macro-like" syntax in the header.
2. NOW I *want* to replace a "cpp-macro" which use the *data-type* as an
argument with a template.
question.
1. Is there a g++ switch to disable all the g++ features except of C
source and template feature ?
2. Is there a gcc switch to add g++-template feature into gcc ?
3. is there an external software to add the c++-template feature into an
existing C software ?
thanks.
What is the latest version og GCC/G++ and how to upgrade? On my version
I can't seem to compile Modern C++ snippets. My version is:

g++ (Rev3, Built by MSYS2 project) 14.1.0
Keith Thompson
2024-12-13 00:39:07 UTC
Reply
Permalink
Chan <***@bghyujhyh.com> writes:
[...]
Post by Chan
What is the latest version og GCC/G++ and how to upgrade? On my version
g++ (Rev3, Built by MSYS2 project) 14.1.0
comp.lang.c++ is a better place for questions about C++.

The latest released version of gcc (which includes g++) is 14.2.0.
There are not likely to be any significant changes between 14.1.0
and 14.2.0.

Your statement that you "can't seem to compile Modern C++ snippets"
is too vague for anyone to be able to help you. You might need
to use the "-std=c++..." option to specify which version of the
C++ standard you want to support. If you still have questions,
start a new thread in comp.lang.c++ (not here!) and include both
the snippets (if they're not too long) and the exact output you
get when you try to complime them.
--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+***@gmail.com
void Void(void) { Void(); } /* The recursive call of the void */
Loading...