469,952 Members | 2,634 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,952 developers. It's quick & easy.

example for define an ordinary function needed.

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

Jul 23 '05 #1
2 1426

baumann@pan wrote:
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

Jul 23 '05 #2

msalters wrote:
baumann@pan wrote:
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 ?


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 )
{

}

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


Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by DiskMan | last post: by
15 posts views Thread by Jorge Naxus | last post: by
10 posts views Thread by Bart Goeman | last post: by
33 posts views Thread by Aaron Watters | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.