template <typename T> void quark(T&& t) {
}
Previous code has that explanation:
When we call
quark(up)
, template argument deduction is performed.quark()
is a function template with a template parameter T, but we haven't provided an explicit template argument (which would look likequark<X>(up)
). Instead, a template argument can be deduced by comparing the function parameter typeT&&
with the function a开发者_JAVA百科rgument type (an lvalue of type string).
Can somebody say me who is who in template argument, template parameter, function parameter, function argument?
May be difference between parameter and argument is what parameter that type wrote in function declaration and argument is entity that actually passed into function? But difference between function and template args/pars i cant even imagine.
The template parameter is T, the template argument is whatever happens to be deduced as T when calling quark(up).
The function parameter is t, the function argument is up.
Template parameter is T
. Template argument is the actual type, the value of T
.
Consider this,
template<typename T, typename U>
void f(T a, U b);
f<int,char>(10, 'A`);
Here T
and U
are template parameters, and int
and char
are template arguments. Since it's a function template, you can also say function template parameters and function template arguments, respectively.
And a
and b
are function parameters, and 10
and 'A'
are function arguments.
Also note that in some situation function template arguments can be deduced by the compiler:
f(10, 'A'); //template arguments can be deduced from 10 and 'A'
Here T
is deduced as int
from the function argument 10
, and U
is deduced as char
from 'A'
.
The interesting difference is that sometime compiler can deduce the function template arguments, but it can never deduce function arguments!
精彩评论