UNIX Socket FAQ

A forum for questions and answers about network programming on Linux and all other Unix-like systems

You are not logged in.

  • Index
  • » C++
  • » template class compiling

#1 2006-11-27 10:55 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: template class compiling

How do you usually work with template classes which have implementation in cpp file?I heard that there is a tricky solution using compiler.Or you just put #inlucde ".cpp"?Or you never do so,but write implementation in .h file?

Offline

#2 2006-11-27 01:21 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: template class compiling

Putting templates in .cpp files is stupid and shouldn't be done, except if they're only used locally. If you don't like the .h file approach then use another extension, like .tmpl or something.

Offline

#3 2006-11-27 02:40 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: template class compiling

Offline

#4 2006-11-27 04:21 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: template class compiling

If you quote whole chunks of an article, at least add a proper reference to the original.

The fact that you shouldn't compile and link those template .cpp files is hint enough that it's the wrong approach. Just don't do it. C++ is ugly enough as it is, no need to make it worse. Get it? Do not make pseudo .cpp files, have some dignity please.

Offline

#5 2006-11-27 05:01 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: template class compiling

Ok ) I just want to write pretty code.

Offline

#6 2006-11-28 01:43 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: template class compiling

What would you say about compiling template classes with unions as template arguments?

Offline

#7 2006-11-28 02:05 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: template class compiling

It makes me wonder who you're trying to upset: yourself, or everyone else who's going to look at that code.

(Btw, moved this thread to the C++ forum, where it belongs.)

Offline

#8 2006-11-28 04:03 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: template class compiling

I was cmiling while reading your answer,but I when i was trying to make map<int,in_addr> I had gotten compilation errors with instantiation.

Offline

#9 2006-11-28 04:53 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: template class compiling

Oh, in that case you should've posted your code, preferably a stripped down version that should compile and shows the problem.

(Before people get the wrong impression: I'm no C++ guru, it is some years ago when I last used it.)

Offline

#10 2006-11-29 10:17 AM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: template class compiling

#include <stdio.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <map>

using namespace std;

typedef in_addr key;
struct obj
{
	int n;
	char c;
};

int main()
{
	obj o;
	o.n	= 123;
	o.c	= 'a';
	map<key,obj> tstMap;
	map<key,obj>::iterator	tstIter;
	char szIp[]="192.168.0.115";
	key keyObj;
	inet_pton(AF_INET,szIp, &keyObj.s_addr);
	tstMap.insert(make_pair(keyObj,o));
	for (tstIter=tstMap.begin();tstIter!=tstMap.end();++tstIter)
	{
		char ip[50];
		inet_ntop (AF_INET, &tstIter->first, ip, sizeof (ip));
		printf("get object ( o.n = %d;\to.c = %c;\nwith key = ip : %s\n",tstIter->second.n,tstIter->second.c,ip);
	}
	return 0;
}

How can I compare two unions?

Offline

#11 2006-11-29 01:21 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: template class compiling

typedef in_addr key;

is wrong, "in_addr" is no type (or is it in C++ all right to leave the struct part away?).

But anyway, the errors occurs because C++ doesn't know how to order in_addr keys. It needs that because it tries to put them in a tree, no matter if you've asked for a map.

In this case you can avoid this mess by adding a struct in_addr field to struct obj, then you don't need a map and everything becomes simpler.

And what are you talking about unions? I don't see any unions involved here.

Offline

#12 2006-11-29 01:29 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: template class compiling

typedef struct in_addr

union
{   
struct {u_char s_b1,s_b2,s_b3,s_b4;} S_un_b;
struct {      u_short s_w1,s_w2;    } S_un_w;
u_long S_addr; 
} S_un;
} in_addr;
MSDN
I wanted to make search by the key (by that union)

Offline

#13 2006-11-29 02:17 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: template class compiling

Well, my /usr/include/linux/in.h tells me:

/* Internet address. */
struct in_addr {
	__u32	s_addr;
};

But the only thing you can be sure of is that in_addr will have a field s_addr, nothing else. When doing POSIX programming it's best not to look at msdn sites too much, considering that MSWindows isn't POSIX compliant...

If you don't add a proper reference for that text you've quoted I'll remove it (it gives bad advice anyway IMHO). Same the next time you post something without proper references.

What's wrong with sticking in_addr into struct obj? Why do you insist on using a map when it isn't needed?

Offline

#14 2006-11-29 02:31 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: template class compiling

in my too
I want to use map,because i want to make search by this field.(In applictaion : get client's in_addr,search (map.find(in_addr) )  and decide if i will work with it or disconnect it.If you know better decision I'll test and implement it, if it works as I need.

Offline

#15 2006-11-29 03:49 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: template class compiling

Are you using UDP or something? Otherwise you can have the socket, inet address and all other client specific data readily available without searching.

But if you really want it and don't mind being IPv4 only, then make a map between your client structure/object and s_addr, which is an int. Else you need to implement a class that has in_addr as data and implements the compare operators (<, >, ==, <=, >= or at least the one the compiler complains about is missing).

Offline

#16 2006-11-29 03:58 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: template class compiling

So I want to do,but as i used to work with windows sockets I thought about comparing windows structure in_addr,which is a union (so it can contain 4 varibales or just 1 (I showed you)).But now I looked at my .h file and found,that it is uint.So I have no problems to implement my plans.Nevertheless it's interesting how to decide such kind of problem even using union as a key in a map.If you tired of my a bit strange and child questiong don't answer - I understand,that you have a lot of work...

Offline

#17 2006-11-29 04:36 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: template class compiling

The problem isn't that it's an union, the problem is that map doesn't know how to compare two such objects. It doesn't work with structures either. So, as I already tried to explain, if you want to use a struct or union with a map, you need to make a wrapper class that contains that struct/union and implements the compare operators. Then use that wrapper class with your map.

Offline

#18 2006-11-29 04:53 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: template class compiling

)
I have already unrestood that!(complier says he doesn't know the result of (myUnionKey & < myUnionKey &)) (Maybe because of my poor english you tell me the same things again)
But how I can compare two unions, if I dont know which data it has?I even think that it's impossible

Offline

#19 2006-11-29 07:29 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: template class compiling

In general you can't, but in this case it was the same data anyway, only split up into bytes, so you could take the int value. But also in general it doesn't matter what the values are, as long as they have an order. So you could always use memcmp on the smallest member of the union (the rest can be garbage if it's unused).

But if you're in such a situation then you probably did make a wrong decision about how to manage your data somewhere, precisely because you don't know what data is in a union.

Offline

#20 2006-11-29 10:03 PM

yurec
Member
From: Singapore
Registered: 2006-11-16
Posts: 134

Re: template class compiling

Thanks!memcmp!that is exactly I want!Now I know,that it's possible.
P.S.Why don't you write where do you live?It's just iteresting for me what is the difference in time between us.

Offline

#21 2006-11-29 10:46 PM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: template class compiling

Apparently in the same timezone as you, or I wouldn't reply that fast. ;-)

Why should I write where I live? I don't think people are very interested in that. But if you want to know, I live in The Netherlands, in a city called Amsterdam.

Offline

#22 2006-11-30 12:19 AM

biologz
Administrator
From: Puking on the pavement
Registered: 2005-11-02
Posts: 396

Re: template class compiling

I knew that, you dirty drug addict :-)


gethostbyintuition() is still a dream of mine

                                                 -- quoted from bash

Offline

#23 2006-11-30 01:23 AM

i3839
Oddministrator
From: Amsterdam
Registered: 2003-06-07
Posts: 2,239

Re: template class compiling

I tend to avoid the tourists infested areas, so no, I'm not. My drugs usage is limited to rare alcohol consumption.

Offline

#24 2006-11-30 02:06 PM

RobSeace
Administrator
From: Boston, MA
Registered: 2002-06-12
Posts: 3,839
Website

Re: template class compiling

Offline

#25 2006-11-30 02:43 PM

duncang
Member
From: UK
Registered: 2004-04-30
Posts: 30

Re: template class compiling

Offline

  • Index
  • » C++
  • » template class compiling

Board footer

Powered by FluxBB