Default argument for templatized function

Jonathan Wakely cow@compsoc.man.ac.uk
Wed May 12 08:32:00 GMT 2004


On Tue, May 11, 2004 at 03:44:35PM -0700, Adrian Bentley wrote:

> Ok so here's my problem, I have this templatized class (with T).  And
> for it I'm defining a templatized member function (with F), I want the
> function to take a comparison operation as a member to make it more
> flexible.
> 
> I'm defining:
> 
> template < typename T >
> class myclass
> {
> public:
>         template < typename F >
>         bool valid(const F & func = std::less<T>()) const
>         {
>                 //... use func as a binary function ...
>         }
> };
> 
> then in my cpp file else where (the instance is using T = float), I'm
> calling:
> 
> myclass_inst.valid()
> 
> and I get this error:
> 
> 2465: no matching function for call to
> `mynamespace::myclass<float>::valid()'

This is a C++ question, not specific to GCC, but I think what's going on
is ...

Because there are no arguments to myclass<T>::valid<F>() the compiler
can't do parameter deduction to work out what F should be, so it doesn't
know which valid<F> you mean, and so it doesn't get as far as checking
the argument list and using the default std::less<float>().
(The name-lookup for valid<F> happens before the argument checking)

I think what you want is something like this (but this is illegal):

    template < typename F = std::less<T> >    // not legal
        bool valid(const F & func = F()) const
        {
                //... use func as a binary function ...
        }

This says that by default type F is std::less<T>, and default constructs
an F() if no argument is given. However, you're not allowed default
template parameters on template functions, so this won't work. See
below for a solution.

> It is bizarre because I'm defining a non-member function below like so:
> 
> template < typename T, typename F >
> void operation(const myclass<T> &r1, const myclass<T> &r2, const F &
> func = std::less<T>())
> {
>         //... compare stuff ...
> }
> 
> and it works just fine!!!!!!  

Even when you call it as "operation(myclass_inst1, myclass_inst2)" ?
The parameter deduction should fail here too.

> Not only that, but if I manually pass that comparison functor in the
> call it works just fine:
> 
> myclass_inst.valid(std::less<float>())

Here you say you want to use valid<F> with F = std::less<float> so the
name-lookup works, and the compiler then moves on to checking the 
function arguments, and using a default constructed std::less<T>().

If you define the member function like this:

    template < typename F >
        bool valid(const F & func = F()) const
        {
            // ...
        }

You can call it like this:

    myclass_inst.valid<std::less<float> >();

But this is a bit verbose: it needlessly duplicates the fact that T=float,
and passing the (default) argument to valid<F>() is redundant if the type
is known. You might prefer this, using a template template parameter and
simply instantiating the functor in the function body rather than taking
an argument:

    template < template <typename> class F >
        bool valid() const
        {
            F<T> f;    // just instantiate F here
            // use f ...
        }
now:
    myclass_inst.valid<std::less>();

(N.B. this only works for functors that take one template parameter,
if that's a problem don't use the template template version)


Finally, to make the functor default to std::less, I'd forget templates
for a moment and overload valid() with a non-template member function:

    bool valid() const { return valid<std::less>(); }

Now myclass<T>.valid() uses std::less<T> , and if you want to use a
different type you use the template function and explicitly name the
type:

    assert( myclass_inst.valid() == myclass_inst.valid<std::less>() );


I think your problem was trying to make one function too flexible and
trying to do everything in one place. Seperate the concerns of the
function template and the default type and it becomes easier.

jon


-- 
"The music business is a cruel and shallow money trench, a long
 plastic hallway where thieves and pimps run free, and good men die
 like dogs.  There's also a negative side."
	- Hunter S. Thompson



More information about the Gcc-help mailing list