🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

templates and function pointers

#25
From: Bala SWAMINATHAN
Date: Mon, 02 Nov 1992 18:24
44 lines
1659 bytes
I have the following template class which has a function ptr variable
"print_item"

I'd like the user to give a function which prints each item. If the
user does not set the value of the print_item variable then I want
the p_item function I define within class.. How to do this?

I did in thefollowing ways and gnu compiler refuses to compile:

template<class T>
class A {
	T	a[100];
	void	p_item(T t) { printf(" %2d",t); }
	void	(*print_item)(T);
public:
	A() { print_item = this->p_item; } // -- says incompatible assignment --
	void	print() { // print all elements of array using "print_item"}
};

OR


template<class T>
void	p_item(T t) {printf(" %2d",t); }

class A {
	T	a[100];
	void	(*print_item)(T);
public:
	A() { print_item = p_item; }
	void	print() { // print all elements of array using "print_item"}
};

-- In this case there is some compiler error in g++ which looks like
cp-class.c:3977: failed assertion `TREE_CODE (TREE_VALUE (rhs)) == FUNCTION_DECL'
/pkg/gnu/bin/gcc: Internal compiler error: program cc1plus got fatal signal 6
*** Error code 1


What am I doing wrong? Or is it not possible to be
too generic with templates at all? Any pointers would be
appreciated. Thank you.

S_Bala
Technical Details
Message-ID: <1992Nov2.182404.1104@wuecl.wustl.edu>
Path: arc1-ca.pugleaf.net!sparky!uunet!gumby!wupost!eclnews!wucs1!bs

Show Full Headers
Complete Article Headers
Newsgroups: gnu.g++.help,comp.lang.c++
Path: arc1-ca.pugleaf.net!sparky!uunet!gumby!wupost!eclnews!wucs1!bs
From: bs@wucs1.wustl.edu (Bala SWAMINATHAN)
Subject: templates and function pointers
Message-ID: <1992Nov2.182404.1104@wuecl.wustl.edu>
Sender: usenet@wuecl.wustl.edu (Usenet Administrator)
Nntp-Posting-Host: wucs1
Organization: Washington University, St. Louis MO
Date: Mon, 2 Nov 1992 18:24:04 GMT
Lines: 45