example for define an ordinary function needed.

B

baumann@pan

hi all,

i am reading the C++ templates complete guide, i have question on
the following statement.

We must therefore make sure the template parameters of the class
template appear in the type of any friend function defined in that
template (unless we want to prevent more than one instantiation of a
class template in a particular file, but this is rather unlikely).
Let's apply this to a variation of our previous example:

template <typename T>
class Creator {
friend void feed(Creator<T>*){ // every T generates a different
... // function ::feed()
}
};

Creator<void> one; // generates ::feed(Creator<void>*)
Creator<double> two; // generates ::feed(Creator<double>*)
In this example, every instantiation of Creator generates a different
function. Note that even though these functions are generated as part
of the instantiation of a template, the functions themselves are
ordinary functions, not instances of a template.


it's here, how can i define the feed() function ?


thanks in advance.

baumann@ pan
 
M

msalters

baumann@pan said:
hi all,

i am reading the C++ templates complete guide, i have question on
the following statement.

"We must therefore make sure the template parameters of the class
template appear in the type of any friend function defined in that
template (unless we want to prevent more than one instantiation of a
class template in a particular file, but this is rather unlikely).
Let's apply this to a variation of our previous example:

template <typename T>
class Creator {
friend void feed(Creator<T>*){ // every T generates a different
... // function ::feed()
}
};

Creator<void> one; // generates ::feed(Creator<void>*)
Creator<double> two; // generates ::feed(Creator<double>*)
In this example, every instantiation of Creator generates a different
function. Note that even though these functions are generated as part
of the instantiation of a template, the functions themselves are
ordinary functions, not instances of a template."
it's here, how can i define the feed() function ?

I'm not sure about your question. feed() looks like
a function call, not a definition. It doesn't call
one of the above-mentioned overloads, which all take
one argument. If it is a definition, it misses a return
type and still has no parameters. Without parameters,
it's unrelated to any of the feed(Creator<T>*) functions.

Those feed(Creator<T>*) functions are defined in the
template itself, BTW. You could specialize Creator, though.

HTH,
Michiel Salters
 
B

baumann@pan

since the functions themselves are ordinary functions , how can i
define function feed?


in my brain, i think feed may be writen as below, but it anyway is not
the "ordinary function" afaik.

template <typename T> void feed(T )
{

}
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top