Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

template specification, when is it needed?

26 views
Skip to first unread message

Doug Mika

unread,
May 11, 2015, 3:04:54 PM5/11/15
to
The std::search function is a template function, however to invoke it, we do NOT need to specify the template parameters!! ie. we can call the function as follows:
std::search (haystack.begin(), haystack.end(), needle1, needle1+4);

So the question is, why in the following program that I have attached, why must I write

size_t nNumEvenElements = count_if(vecIntegers.begin(), vecIntegers.end(), IsEven<int>);

why can't I write

size_t nNumEvenElements = count_if(vecIntegers.begin(), vecIntegers.end(), IsEven);


The program:
#include <iostream>
#include <string>
#include <map>
#include <functional>
#include <algorithm>
#include <vector>

template <typename elementType>
bool IsEven (const elementType& number){
return ((number % 2)==0);
}

int main(int argc, char** argv) {

//The Program
using namespace std;
vector<int> vecIntegers;

for(int i=-9; i<10; ++i)
vecIntegers.push_back(i);

size_t nNumEvenElements = count_if(vecIntegers.begin(), vecIntegers.end(), IsEven<int>);

cout<<"Number of Even Elements is: "<<nNumEvenElements<<endl;

//after my code returns from test() should I avoid using y?)
return 0;
}

Victor Bazarov

unread,
May 11, 2015, 3:44:44 PM5/11/15
to
Have you given any thought to a possibility of getting a decent book
that would explain those things? I mean, for example, "C++ Templates"
by Vandevoorde and Josuttis.

'IsEven' is not an expression that yields an object from which the
compiler can deduce the template argument when it instantiates the
'count_if' function template. 'IsEven<int>' *is* such an expression.
The function template 'count_if' is written to expect a *type* as its
second template argument. That type can be deduced from the third
argument of the function call, *if* that argument is an expression that
has a type.

I wonder how many times I would need to tell you that before you start
paying attention :-)

Get a decent book and *study*. Systematically and *attentively*.

V
--
I do not respond to top-posted replies, please don't ask

Richard

unread,
May 11, 2015, 4:02:17 PM5/11/15
to
[Please do not mail me a copy of your followup]

Victor Bazarov <v.ba...@comcast.invalid> spake the secret code
<mir0p1$lt4$1...@dont-email.me> thusly:

>Have you given any thought to a possibility of getting a decent book
>that would explain those things? I mean, for example, "C++ Templates"
>by Vandevoorde and Josuttis.

While I like that book, it's probably overkill for this sort of
problem. This stuff is covered pretty well in Stroustrup's "C++
Programming Language" 4th edition. This is a basic question about
templates, not something you need the extensive detail of "C++
Templates" to answer.

(Aside: "C++ Templates" came out in 2002 and I recently re-read it and
a bunch of the stuff mentioned about compilers and weak support, etc.,
is no longer true. Another reason it's probably not the best place to
send a n00bie. Josuttis's "The C++ Standard Library" has recently
been updated and seems to cover this pretty well in 6.8 Functions as
Algorithm Arguments.)
--
"The Direct3D Graphics Pipeline" free book <http://tinyurl.com/d3d-pipeline>
The Computer Graphics Museum <http://computergraphicsmuseum.org>
The Terminals Wiki <http://terminals.classiccmp.org>
Legalize Adulthood! (my blog) <http://legalizeadulthood.wordpress.com>

Paavo Helde

unread,
May 11, 2015, 4:17:16 PM5/11/15
to
Doug Mika <doug...@gmail.com> wrote in
news:bb027818-8e62-4622...@googlegroups.com:

> The std::search function is a template function, however to invoke it,
> we do NOT need to specify the template parameters!! ie. we can call
> the function as follows: std::search (haystack.begin(),
> haystack.end(), needle1, needle1+4);
>
> So the question is, why in the following program that I have attached,
> why must I write
>
> size_t nNumEvenElements = count_if(vecIntegers.begin(),
> vecIntegers.end(), IsEven<int>);
>
> why can't I write
[...]
> template <typename elementType>
> bool IsEven (const elementType& number){
> return ((number % 2)==0);
> }
[...]
>
> size_t nNumEvenElements = count_if(vecIntegers.begin(),
> vecIntegers.end(), IsEven);

The template which you need to explicitly instantiate is not
std::count_if(), but your own IsEven. If you don't like to do this, then
one option is simply to not use a template.

More to the point, template parameters can be omitted if the compiler is
able to deduce them from the types of passed function arguments. In the
above line no arguments are passed to IsEven, so the compiler cannot
deduce its template parameters. So it does not know what type is the
argument for the count_if and so in turn it is not able to deduce the
template paramters for the count_if itself.

You can "fix" this by specifying the count_if template parameters
explicitly. Then you don't need to specify them for IsEven:

size_t nNumEvenElements =
count_if<vector<int>::iterator, bool (*)(const int&)>(
vecIntegers.begin(), vecIntegers.end(), IsEven);


hth
Paavo

Juha Nieminen

unread,
May 12, 2015, 3:11:51 AM5/12/15
to
Doug Mika <doug...@gmail.com> wrote:
> The std::search function is a template function, however to invoke it,
> we do NOT need to specify the template parameters!!

With template functions you don't need to specify the template
parameters if these parameters appear in the function's parameter
list, and the compiler can deduce the template parameters from the
call. However, with template classes you have to explicitly tell
the template parameter because there's nothing in the instantiation
of the class that would tell it otherwise. (Also if a template
function does not use the template parameters anywhere in the
function parameters, you also have to say it explicitly.)

--- news://freenews.netfront.net/ - complaints: ne...@netfront.net ---
0 new messages