Archived

This topic is now archived and is closed to further replies.

[Template] Function dependencies?

This topic is 5588 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I have an octree template. It stores pointers to spatial objects in the tree. However once I go with templates I no longer need to specify the base interface class that enforces the types shape. For example I call Node->GetSphere(); in my code (Where Node is the type). Is this sloppy or the way it''s meant to be? I mean the compiler _does_ pick it up if I don''t define needed functions, not the the message is 100% intuitive, but I am getting use to it. Thoughts? Chris Brodie http:\\fourth.flipcode.com

Share on other sites
I think I know where you are coming from, ...ish.
1. If a templated function is never used then it will not be compiled into the code.
2. If you fail to impliment a function in the class that gets templated then it will cause an error, however there are a couple of ways to check at compile time that this is true... ie:

The microsoft way.... (MSVC specific)

        template< class T > struct foo{	void Func() {		__if_exists(Function) {			Function();		}		__if_not_exists(Function) {			cout << "no func defined" << endl;		}	}};struct bar{	void Function();};

The above is quite funky but you still have no way to guarentee that the function matches the correct type required. In this case you can use some compile time assertions.....

              // declare a template function (though it never gets called, the// params and return types are evaluated at compile time)template <class T> char ERROR_Types_Are_Not_The_Same( T );// a little macro to take the type, the variable itself and an// error message if the variable doesn't match the type...#define CTASSERT_TYPE( _TYPE_, _VAR_, _MSG_ ) { 	typedef _TYPE_ _MSG_##_##_VAR_;		\		(void)sizeof( ERROR_Types_Are_Not_The_Same < _MSG_##_##_VAR_ >( _VAR_ ) ); }// our foo struct can now do something like this to ensure that the Function type matches the type we are expecting....template< class T > struct foo{	void Func() {		__if_exists(Function) {			// make a little typedef for the function pointer type			typedef void (*Func)(void);			// a compile time check that incurs zero overhead and compares the function pointers			// to ensure they are compatible...			CTASSERT_TYPE(Func,Function,Function_Is_Not_Of_Correct_Type);			Function();		}	}};

Basically this works on the principle that everything inside of the sizeof() operator is only checked at compile time. This essentually means that it all gets compiled out of the code and therefore incurs zero overhead. You can do this a bit more elegantly if you have access to partial template specialisation. I'd advise buying the book Modern C++ Design by Andrei Alexandrescu: ISBN 0-201-70431-5 it is by far the best book available on the subject and explains the ideas a lot better than I can.

Hope that's of some use....

[edited by - RobTheBloke on August 27, 2002 10:22:42 AM]

[edited by - RobTheBloke on August 27, 2002 10:23:36 AM]

[edited by - RobTheBloke on August 27, 2002 10:24:28 AM]

Share on other sites
check the back slashes on the macro the source tags just seem to remove them....

• Forum Statistics

• Total Topics
628694
• Total Posts
2984253

• 16
• 13
• 13
• 10
• 10