473,387 Members | 1,578 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 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 1582

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Bo Xu | last post by:
Object of Combination By Bo Xu Introduction A combination of n things, taken s at a time, often referred as an s-combination out of n, is a way to select a subset of size s from a given set of...
1
by: DiskMan | last post by:
System: Redhat 7.2 Kernel-2.6.11.8 GCC-3.4.3 CCC-6.5.9 Binutils-2.15 Make-3.80 GTK/GLIB-2.6.7 For some reason my Linux box is suddenly having issues trying to read ;
2
by: FAN | last post by:
I want to define some function in python script dynamicly and call them later, but I get some problem. I have tried the following: ################################## # code...
15
by: Jorge Naxus | last post by:
Hello I would like to write a macro like that: #ifdef DEBUG #define printj(...) printf(...) #else #printj(...) #endif
10
by: Bart Goeman | last post by:
Hi, I have a question about how to put redundant information in data structures, initialized at compile time. This is often necessary for performance reasons and can't be done at run time (data...
42
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
69
by: RC | last post by:
I know how to do this in JavaScript by window.open("newFile.html", "newTarget", "scrollbars=no,resizable=0,width=200,height=200"); The browser will open a new window size 200x200, not allow...
6
Xx r3negade
by: Xx r3negade | last post by:
Consider this: I have the class foo, with the public method thread(), and the private method bar() In the thread() method, I would like to create a new thread, using pthread_create(), that...
33
by: Aaron Watters | last post by:
Why is the migration to py3k a concern? For example I have libraries which use string%dictionary substitution where the dictionary is actually an object which emulates a dictionary. The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.