Topic: Template function matching


Author: bglenden@colobus.cv.nrao.edu (Brian Glendenning)
Date: Tue, 1 Nov 1994 20:11:08 GMT
Raw View
Often one wants to trade off between inline function calls and
template instantiaions. Consider the following template functions:

template<class function_type> void foo(const function_type &value) // 1
{
    cout << "generic " << value() << endl;
}

template<class type> class func
{
public:
    virtual type operator()() const = 0;
};

template<class type> void foo(const func<type> &value)            // 2
{
    cout << "func " << value() << endl;
}

The first one is to be template expanded for any entity to which "()"
returns a value. The second one is only intended to be called for
objects derived from class func<type>, which has operator() defined
upon it. It is only template expanded for the possible template types,
not for all classes.

The advantage of 1 is that () might result in an inline function call,
and it can be used for "native" types, e.g. function pointers.

The primary advantage of 2 is that it might result in far fewer
template instantiations.

Presently the compilers I have access to (CFront 3.0.2, Sun 4.0.1, IBM
xlC ?.?.?) all choose 1 when given an object derived from func. I know
that the standardization effort has been looking at the matching
rules. Can anybody tell me
 o Whether the above usage will work in the future; or
 o If there's another way to get the same result?

Thank you.

Brian
--
       Brian Glendenning - National Radio Astronomy Observatory
bglenden@nrao.edu          Charlottesville Va.           (804) 296-0286