473,382 Members | 1,365 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

templated class implementation in C

Hello,
I would like to know how to implement template classes in C. Does
someone know of any resources which I can read.

Thanks
Jami

Feb 20 '06 #1
19 1496

jamihuq wrote:
Hello,
I would like to know how to implement template classes in C. Does
someone know of any resources which I can read.


C doesn't support templates and doesn't have class types and you are in
the wrong group.

Feb 20 '06 #2

jamihuq wrote:
Hello,
I would like to know how to implement template classes in C. Does
someone know of any resources which I can read.


Try this guy. I think he tried it once upon a time.

http://public.research.att.com/~bs/homepage.html

regards
Andy Little

Feb 20 '06 #3
This is the same sort of reply I got from another helpful person when I
wanted some resources converting C++ class to C. But alas, there was a
way to implement in C C++ classes. So, if you don't know try being a
little less rude and not reply.

Feb 20 '06 #4
jamihuq wrote:
This is the same sort of reply I got from another helpful person when I
wanted some resources converting C++ class to C. But alas, there was a
way to implement in C C++ classes. So, if you don't know try being a
little less rude and not reply.


Well, it can't be done. What would you like him to say?

You could use something like PERL to parse the files before compiling
them, I suppose. But that's not "in C".

It's also off topic here.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 20 '06 #5
jamihuq wrote:
I would like to know how to implement template classes in C. Does
someone know of any resources which I can read.


A book by Kernighan and Ritchie, maybe?

V
--
Please remove capital As from my address when replying by mail
Feb 20 '06 #6
jamihuq <ja*********@yahoo.com> wrote:
Hello,
I would like to know how to implement template classes in C. Does
someone know of any resources which I can read.


I didn't see anything about templates, but for implementing classes in C
(including inheritence, virtual functions, and runtime-polymorphism),
the following links may provide insight:

Alf P. Steinbach's excellent Pointers document:
http://home.no.net/dubjai/win32cpptu...ters/ch_01.pdf

Event Helix articles:
http://www.eventhelix.com/RealtimeMa...ject_Oriented/

--
Marcus Kwok
Feb 20 '06 #7
I bet it can be done since C++ code, just like C is eventually mapped
into Assembly. So, if C++ is transformed to Assembly, then definitely
templates can be implemented in C.

Feb 20 '06 #8
jamihuq wrote:
I bet it can be done since C++ code, just like C is eventually mapped
into Assembly. So, if C++ is transformed to Assembly, then definitely
templates can be implemented in C.


Templates are a form of code generation, instantiated before compile
time after the template (and specialisation of template) has been
deduced, by some type of preprocessor.

C does not have any of that.

Think about it.

C++ and C are completely different languages, with differing capabilities.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 20 '06 #9
jamihuq wrote:
I bet it can be done since C++ code, just like C is eventually mapped
into Assembly. So, if C++ is transformed to Assembly, then definitely
templates can be implemented in C.

The best you can do is use macros as pretend template functions or
struct member functions. It can get horribly messy.

--
Ian Collins.
Feb 20 '06 #10
jamihuq wrote:
I bet it can be done since C++ code, just like C is eventually mapped
into Assembly. So, if C++ is transformed to Assembly, then definitely
templates can be implemented in C.


The problem that you will run into is that C++ templates are
"instantiated" before being compiled into assembly. I think that what
you want to accomplish is possible, but it's complicated enough that I
wouldn't even consider it; I'd just use C++.

That being said, the way to accomplish it would be to write a
"preprocessor" that would would turn templates into .c files, and
provide instantiation, and let the program know that it had some more
object files to link in. To accomplish that, you would need
expert-level expertise in templates. I think "C++ Templates - The
Complete Guide"
by Vandevoorde and Josuttis would be a good place to start.

Maybe the original CFront source code is floating around somewhere.

good luck,
~KS

Feb 20 '06 #11

jamihuq wrote:
I bet it can be done since C++ code, just like C is eventually mapped
into Assembly. So, if C++ is transformed to Assembly, then definitely
templates can be implemented in C.


The same could be said of Lisp or Brainfuck. Why would a C++
programmer know? Go ask the C programmers.

Feb 20 '06 #12
I told you guys it can be done. And here is the resource where it
describes how to do it. Yes I understand about the compile time
instantiation, but I wanted a method to replicate the behavior of
templates without writing code for each data type passed in.

I guess Victor knows more than anyone else :)

Thanks Victor

http://home.no.net/dubjai/win32cpptu...ters/ch_01.pdf

Feb 20 '06 #13
Oops, I meant Marcus

Feb 20 '06 #14

jamihuq wrote:
I told you guys it can be done. And here is the resource where it
describes how to do it. Yes I understand about the compile time
instantiation, but I wanted a method to replicate the behavior of
templates without writing code for each data type passed in.

I guess Victor knows more than anyone else :)

Thanks Victor

http://home.no.net/dubjai/win32cpptu...ters/ch_01.pdf


I don't see anywhere in that document that describes how to implement
templates in C or anything remotely resembling templates for that
matter.

Feb 20 '06 #15
jamihuq wrote:
Oops, I meant Marcus


What is this supposed to mean? Read the information in my .sig.

Brian

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Feb 21 '06 #16
> Try this guy. I think he tried it once upon a time.

http://public.research.att.com/~bs/homepage.html


I thought he tried it before templates came into C++ :)

Ben
Feb 21 '06 #17
Oh, and here is the answer from Stroustrup himself:
************************************************** ************************************
template<class T> struct X { T a; void f(T); };

X<int> x; f(x);

struct X_int { int a; void f(int); };
X_int x; f(x);

You might be interested in the C++ standard committee's report on
performance (see my C++ page). I explains some of the implementation
techniques.
************************************************** *****************************************
I figured this is a way to implement templates, but I just wanted to
make sure from the man that wrote C++. Goes to show, the ones that
shout the most and use profanity are usually the monkeys that can't
even peel a banana.

..

Feb 22 '06 #18
jamihuq wrote:
Oh, and here is the answer from Stroustrup himself:
************************************************** ************************************
template<class T> struct X { T a; void f(T); };

X<int> x; f(x);

struct X_int { int a; void f(int); };
X_int x; f(x);

You might be interested in the C++ standard committee's report on
performance (see my C++ page). I explains some of the implementation
techniques.
************************************************** *****************************************
I figured this is a way to implement templates, but I just wanted to
make sure from the man that wrote C++. Goes to show, the ones that
shout the most and use profanity are usually the monkeys that can't
even peel a banana.


The whole purpose of templates is so that you don't have to repeat the
same thing for each type.

If you class manually repeating the same code for each type templates in
C, then yes. Well done.

C does not have language support for templates. C does not have support
for classes.

Yes, with effort (lots of it) you can mimic some of the concepts, but
that's not the point.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Feb 23 '06 #19
In real life, everything is not supposed to be straight forward. If you
are programming in an embedded environment, sometimes you don't have
the luxury of getting an OS that supports C++. And if the OS does
support C++, it is incomplete.

Feb 24 '06 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: tirath | last post by:
Hi all, I have a templated class that derives from a non-templated abstract class. How do I then cast a base class pointer to a <templated> derived class pointer in a generalised fashion? ...
1
by: Rich | last post by:
Hi, I have a query regarding VC6 and its handling of templated copy constructors. Here goes: Take a look at the following code sample... template<class _Ty, size_t t_uiSize = 10 > class...
1
by: Vince | last post by:
Hi, I have a templated class implemented in one file called CDynWnd.h and declared like this : CDynWnd.h (declaration and implementation) ------------ template <class BASECLASS> class...
2
by: Alex Drummond | last post by:
Hello, Is there any way of specializing a templated function on a type which is itself templated? Here's the simplest example of the problem I can think of. Say I have written an implementation...
0
by: John Crowley | last post by:
I keep running into this over and over again... I want a block server control that renders a header and footer, and child controls in between. But I don't want a templated control, for the...
11
by: Matthias | last post by:
Hello, templated virtual member functions are not allowed. Now I am searching for a good workaround for this problem, but I can't find any. Here's my problem: class Base { template...
2
by: David O | last post by:
I am using the CRTP (Curiously Recurring Template Pattern) to provide a set of low-level functions customizable by environment. Using CRTP ensures that all the function have the same signatures,...
12
by: Robert.Holic | last post by:
Hi All (first time caller, long time listener), I've stumbled across a problem that I have yet to figure out, although Im sure I'll kick myself when I figure it out. Here it is: I need to...
6
by: mathieu | last post by:
Hi there, I am currently struggling with a compiler error on a templated code. The error is complete non-sense. I think the issue here is that I am including files in a loop. To handle templates...
2
by: domehead100 | last post by:
I have a templated class, CDerived: template <typename TValue, typename TDraw, typename TEdit ...> class CDerived : public CBase { TValue m_Value public: TValue& GetValue() const {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.