TITLE: Default parameters vs overloading


PROBLEM: mg@tyrolia (Michael Golan)


What's the net wisdom on a default parameter which is a class? ...

My best solution is 

T Default_T(0) ;	// global default class T

and

void f(...., const T& = Default_T);

But this goes "against"  OO, and pollute the name space. Anyone has a better
solution (trick?)



RESPONSE: steve@taumet.com (Steve Clamage), 16 Nov 92
	  Vice Chair, ANSI C++ Committee, X3J16

I suggest not using default parameters, but using overloaded functions
instead.  This avoids quite a few problems associated with default
parameters.  Example:
	void f(int, const T&);

	void f(int i)
	{
	    static T t;
	    f(i, t);
	}

If you can't stand the overhead of an extra function call, you could
combine this with someone else's suggestion of a static class member
(because an inline function can't have static local variables).
Example:

	class T {
	public:
	    static T t;	// for default parameters of type T
	    ...
	};
	void f(int, const T&);
	inline void f(int i) { f(i, T::t); }

When default parameters are not simple literals, they sometimes result
in unexpected behavior due to scope interactions, and sometimes
interact unexpectedly with function overloading.  Using explicit
overloading instead as above often results in more reliable and
maintainable programs.
