Template argument deduction

P

Peng Yu

Hi All,

In the book
Working Paper for
Draft Proposed International Standard for Information Systems$)A!*
Programming Language C+ +,
template argument deduction is discussed.

However, the discussion in the above book is too abstract to read.

My question is:
For example, although the definition of bind2nd is

template <class _Operation, class _Tp>
inline binder2nd<_Operation>
bind2nd(const _Operation& __oper, const _Tp& __x)
{
typedef typename _Operation::second_argument_type _Arg2_type;
return binder2nd<_Operation>(__oper, _Arg2_type(__x));
}

we can still call bind2nd without specify _Operation and _Tp
explicitly like.
bind2nd(greater<int>(), 100).


I'm wondering if there any books or webpages which discuss this issue
in great details and provide many examples.

Best wishes,
Peng
 
M

Mike Wahler

Peng Yu said:
Hi All,

In the book
Working Paper for
Draft Proposed International Standard for Information Systems$)A!*
Programming Language C+ +,
template argument deduction is discussed.

However, the discussion in the above book is too abstract to read.

My question is:
For example, although the definition of bind2nd is

template <class _Operation, class _Tp>
inline binder2nd<_Operation>
bind2nd(const _Operation& __oper, const _Tp& __x)
{
typedef typename _Operation::second_argument_type _Arg2_type;
return binder2nd<_Operation>(__oper, _Arg2_type(__x));
}

we can still call bind2nd without specify _Operation and _Tp
explicitly like.
bind2nd(greater<int>(), 100).


I'm wondering if there any books or webpages which discuss this issue
in great details and provide many examples.

This concept is known as 'template argument deduction', i.e. in
many cases (but not all), the compiler can deduce the proper types
from the context of the function call (in those cases which it cannot,
it might either choose the wrong instantiation, or issue a diagnostic.
I don't know of any online resources about this, but two books I have
that discuss this imo rather well are:

http://www.josuttis.com/tmplbook/index.html ("Vandevoorde/Jousttis")
http://tinyurl.com/3ot7j ("Austern")

IMO both are good books, but the first goes into greater detail about
the actual mechanics of how C++ templates work. The second is mostly
oriented around practical applications of templates and use of the 'STL'
(the standard library container types).

HTH,
-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,012
Latest member
RoxanneDzm

Latest Threads

Top