hey all

if i define:

		///Reference version  
		TalkyBase&	operator<<(TalkyMessage& msg);  
		///Instance version  
		TalkyBase&	operator<<(TalkyMessage m) {  
			(*this) << m;  

then i get an ambiguity error with

TalkyMessage m;  
talkyNode << m;  

but the issue is that i want to call the reference version when calling like that.
but when necessary, call the instance version, e.g.

talkyNode << TalkyMessage(shapeSetRequest);  

generally i’m quite interested in this overloading thing
since i’m using a lot of cases where i’m having general cases vs specific cases, e.g.

template <class T>  
TalkyMessage operator<<(T& object);  
TalkyMessage operator<<(Shape& s);  

Are there any references / keywords for looking up these kinds of issues?

rain in :slight_smile:


can’t you use?

TalkyBase&  operator<<(const TalkyMessage& msg);    

unless you really need to modify the msg it should do for both cases

Cheers Arturo!

Any idea about the second issue?

so i’ve got a general (template) case

template <class T>  
TalkyMessage operator<<(T& object);  

but I also want to use a special case in some instances.
Ideally i’d like to have an interface called TalkySerialisable with public methods

class TalkySerialisable {  
    virtual void serialise(TalkyMessage &m) const = 0;  
    virtual bool deSerialise(TalkyMessage &m) = 0;  

then for instance

TalkyMessage msg;  
ClassWithPointers thingyContainingPointers; // inherits TalkySerialisable  
msg << ofPoint(0.0f, 0.0f); // calls general (template) case)  
msg << thingyContainingPointers; // calls internal serialise function defined in TalkySerialisable. i.e. we override the general case to the specific case  

Is there a reliable method for determining which takes priority? general case or specific case.

Yes. Look for “template specialization”.
You should be able to do something like (haven’t tried compiling that, though)

template <class T>    
class TalkyStuff {  
TalkyMessage operator<<(T& object);  //general << op  
template <>  
class TalkyStuff <ClassWithPointers> {  
TalkyMessage operator<<(ClassWithPointers& object);  //specialized << op   

See http://www.cplusplus.com/doc/tutorial/templates/- http://www.cprogramming.com/tutorial/template-specialization.html

yes, that should work, and the specialized version takes always preference

cheers bilderbuchi!

i was after specialising the function
my code is like this now

	class TalkyBuffer;  
	class TalkySerialisable {  
		virtual void	serialiseToBuffer(TalkyBuffer &b) const = 0;  
		virtual bool	deSerialiseFromBuffer(TalkyBuffer &b) = 0;  
	class TalkyBuffer : TalkySerialisable {  
		template<class T>  
		TalkyBuffer& operator<<(T const &object) {  
			if (!write(&object, sizeof(T)))  
				throw("Buffer overrun - insufficient space to write");  
			return *this;  
	///Used e.g. when putting a payload onto main buffers  
	template<> TalkyBuffer& TalkyBuffer::operator<<<TalkyBuffer>(TalkyBuffer const &other);  

I found that gcc only allows template specialisation for functions in a namespace but not inside a class. however this works fine (apparently). recoding a lot at once so will rain back in if it doesn’t work as expected later.

maybe that is relevant?

“It turns out that there’s a provision in the C++ spec that explicitly disallows specializing a template class or function nested inside of a template class unless you also explicitly specialize the outer template as well. Visual Studio doesn’t enforce this rule, hence the confusion with the previous example, but g++ certainly does.”
from http://stackoverflow.com/questions/4944156/c-method-specialization-and-templates


interesting reading!
this is the link where i found out about specialising template functions aside from templated classes:

to clarify for future readers, this is template specialisation of a templated method, inside a non-templated class.

EDIT: if you are looking for template specialisation of a function inside a templated class, then check out bilderbuchi’s last 2 links:


cheers for that link, I’m reading it now. (edit: whoa, step off the deep end indeed… nice technique at the end of the article)

Personally, I’ve found learncpp.com (for basic and syntax questions) and stackoverflow.com (for any problems/further questions that come up) to be sufficient for me around 90% of the time, so I go there first to find answers. :slight_smile:
Additionally, I found this book to be an awesome resource, I’m currently reading through it. Loads of stuff to learn. If you want to learn C++, it’s a worthwhile investment (though not a C++ basics book!)
http://www.amazon.de/Effective-Specific-Addison-Wesley-Professional-Computing/dp/0321334876/ref=sr-1-1?ie=UTF8&qid=1313567284&sr=8-1 (be sure to get the 2005 edition)