472,096 Members | 2,041 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,096 software developers and data experts.

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 1509

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
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.