Re: The first template argument
Am 21.01.2012 15:11, schrieb kelvSYC:
[..]
But what if I have this:
template<class T2, class T1>
T1 foo(const T2&);
Again, I'd have to state what T1 is since the compiler might not
figure it out on its own. But calling foo<int>() and passing in a T2
appears to be an error according to my compiler (is it because I am
specifying T2 rather than T1?).
Your intuition is correct, you have just provided T2 in this example,
because template arguments - like all other forms of arguments in C++
have a specified order. How should the compiler know which parameters
you meant? This second example is a good example for a badly designed
interface: Every user is required to provide both template arguments to
make the compiler happy.
Is there something privileged about
the first template argument that makes the first valid and the second
not?
Not at all. But the rules are as follows: Unless you provide any
explicit arguments, the compiler will try to deduce the parameter types.
This won't work here, so you need to provide explicit arguments.
Providing explicit arguments means that you start from the most left one
and each not provided ones are again deduced (when possible). But the
compiler won't guess *which* argument you meant, explicit arguments are
in strict parameter declaration order.
Also, related to this is suppose now that function template is part of
a functor:
struct Functor {
template<class T1, class T2> T1 operator()(const T2&);
};
I'd like to know if f is a Functor, whether f<int>(bar) is a valid
expression (and if not, how to fix), and how the things above apply in
this situation (ie. swapping T2 and T1 in the template declaration).
The situation here is a bit different, the expression f<int>(bar) is
ill-formed, because this would be evaluated as if f would be a function
template. You have here a member operator template with the full name
operator(), therefore you need to write it in the full ugly form
f.operator()<int>(0);
when you need to provide a template parameter like in this case. I would
try to stay away of such a design, because it makes it impossible to use
the operator short hand form. Either make Functor a template, like
template<class T1>
struct Functor {
template<class T2> T1 operator()(const T2&);
};
or replace operator() by a normal member function template, e.g.
struct Functor {
template<class T1, class T2> T1 call(const T2&);
};
and invoke it via
f.call<int>(bar)
HTH & Greetings from Bremen,
Daniel Kr?gler
--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]