🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

1 total messages Started by Pablo Hernan Gelaf Tue, 09 Jan 1996 12:05
Templates & inlines
#873
Author: Pablo Hernan Gelaf
Date: Tue, 09 Jan 1996 12:05
87 lines
2802 bytes
I have the following code that implements arithmetic operators using
templates;

--------8<--------------------------------------------------
#include <stdio.h>

class A {

private:
	
	int i;
		
public:

	A(int j)
	:	i(j)
	{ }
	
	friend inline int cmp(const A &a1, const A &a2);
};

inline int cmp(const A &a1, const A &a2)
{
    return a1.i - a2.i;
}

template<class T> inline bool operator==(const T &op1, const T &op2)
{
	return cmp(op1, op2) == 0;
}

template<class T> bool operator<(const T &op1, const T &op2)
{
	return cmp(op1, op2) < 0;
}

template<class T> bool operator>(const T &op1, const T &op2)
{
	return cmp(op1, op2) > 0;
}

template<class T> bool operator<=(const T &op1, const T &op2)
{
	return cmp(op1, op2) <= 0;
}

template<class T> bool operator>=(const T &op1, const T &op2)
{
	return cmp(op1, op2) >= 0;
}

int main(int argc, char **argv)
{
	A a1(1), a2(2);
	
	for (int i = 0; i < 10000000; i++)
		if (a1 == a2)
			fprintf(stderr, "a1 == a2\n");
	
}
--------8<--------------------------------------------------

Comparing (in term of time) this implementation with another that 
does not use templates I can figure that the 'operator==' is not being 
compiled as an inline function. I'm compiling on a SGI (IRIX 5.3)
using gcc2.7.0 with the '-O2' flag.

Is there any flag that I can use to turn on the inline capability in 
templates functions ?

Thanks.
-- 
-----------------------------------------------------------------
Pablo H. Gelaf                           InterSoft Argentina S.A.
Senior Software Engineer                  Av. Cordoba 883 Piso 13
pablog@isoft.com.ar               (1054) Buenos Aires - Argentina
http://www.isoft.com.ar/~pablog             Voice: +54-1-318-8900
                                              Fax: +54-1-318-8999
-----------------------------------------------------------------
-- 
-----------------------------------------------------------------
Pablo H. Gelaf                           InterSoft Argentina S.A.
Senior Software Engineer                  Av. Cordoba 883 Piso 13
pablog@isoft.com.ar               (1054) Buenos Aires - Argentina
http://www.isoft.com.ar/~pablog             Voice: +54-1-318-8900
                                              Fax: +54-1-318-8999
-----------------------------------------------------------------
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads