🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.g++.help
Article #873

Templates & inlines

#873
From: 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
-----------------------------------------------------------------
Technical Details
Message-ID: <m0tZc7J-000CBoC@gizmo>
Path: arc1-ca.pugleaf.net!gwaihir.isoft.com.ar!pablog

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!gwaihir.isoft.com.ar!pablog
From: pablog@gwaihir.isoft.com.ar (Pablo Hernan Gelaf)
Newsgroups: gnu.g++.help,comp.lang.c++
Subject: Templates & inlines
Date: 9 Jan 1996 07:05:12 -0500
Organization: Gatewayed from the GNU Project mailing list help-g++@prep.ai.mit.edu
Sender: daemon@cis.ohio-state.edu
Distribution: world
Message-ID: <m0tZc7J-000CBoC@gizmo>
Reply-To: pablog@isoft.com.ar