This is my first post here, please tell me if I did anything wrong.
in the following code snippet:
1. template <class In, class Pred>
2. In find_if(In begin, In end, Pred f) {
3. while (begin != end && !f(*begin))
4. ++begin;
5. return begin;
6. }
7. bool is_negative(int n) {
8. return n < 0;
9. }
10. vector<int>::it erator i = find_if(v.begin (), v.end(),
is_negative);
find_if is the template needs a class Pred as its third argument but
in line 10's function call, it takes a function poniter is_negative,
does that mean in C++, function point is equivalent to class?
Thank you. 2 2379
On 2007-10-15 21:09, we*********@gma il.com wrote:
This is my first post here, please tell me if I did anything wrong.
in the following code snippet:
1. template <class In, class Pred>
2. In find_if(In begin, In end, Pred f) {
3. while (begin != end && !f(*begin))
4. ++begin;
5. return begin;
6. }
7. bool is_negative(int n) {
8. return n < 0;
9. }
10. vector<int>::it erator i = find_if(v.begin (), v.end(),
is_negative);
find_if is the template needs a class Pred as its third argument but
in line 10's function call, it takes a function poniter is_negative,
does that mean in C++, function point is equivalent to class?
No. If you rewrite the template thingie like this instead
template <typename In, typename Pred>
In find_if(In begin, In end, Pred f)
it might become a little more clear, the types of In and Pred are
determined when the code is compiled, and it does not have to be a
class, anything that is a type will do.
If we take you code and add (before line 10) the following:
struct IsNegative {
bool operator()(int n) { return n < 0; }
};
And after line 10 we add
IsNegative pred;
vector<int>::it erator i2 = find_if(v.begin (),v.end(), pred);
Now, in the first call the type of Pred in find_if() was a pointer to a
function, while in the second call the type is IsNegative. This process
where the types of the parameters are decided is called instantiation.
The template is not code that can be executed, it is just a template
describing a generic algorithm (or class), to be useful we must use this
description and fill in the parameters with real types, which created a
real function (or class). So with the additions that I made your program
will now contain two find_if functions, the first has the following
signature:
typedef vector<int>::it erator iter;
iter find_if(iter, iter, bool (*)(int));
Notice that the last argument is a function pointer. The second find_if
function have this signature:
iter find_if(iter, iter, IsNegative);
The last argument is now an instance of the IsNegative class.
Hope this clear some things up for you.
--
Erik Wikström
On Oct 15, 9:50 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
On 2007-10-15 21:09, webinfin...@gma il.com wrote:
This is my first post here, please tell me if I did anything wrong.
in the following code snippet:
1. template <class In, class Pred>
2. In find_if(In begin, In end, Pred f) {
3. while (begin != end && !f(*begin))
4. ++begin;
5. return begin;
6. }
7. bool is_negative(int n) {
8. return n < 0;
9. }
10. vector<int>::it erator i = find_if(v.begin (), v.end(),
is_negative);
find_if is the template needs a class Pred as its third argument but
in line 10's function call, it takes a function poniter is_negative,
does that mean in C++, function point is equivalent to class?
No. If you rewrite the template thingie like this instead
template <typename In, typename Pred>
In find_if(In begin, In end, Pred f)
it might become a little more clear, the types of In and Pred are
determined when the code is compiled, and it does not have to be a
class, anything that is a type will do.
Anything which fulfills the constraints of the template. In
this case, any type which can be called ("()" operator) with a
single argument of the iterator value type, and returns
something which converts to bool.
For various historical reasons, the () operator can be applied
to a pointer to a function, as well as to a function. In
pre-template days, this was often considered a defect in the
language---a flaw in the type system. Serendipitously , it turns
out to be an advantage with templates, where duck typing is the
rule.
And of course, in C++, a pointer to a function type is an object
type, just like a class.
--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Surya Kiran |
last post by:
Hi all,
I've written a function template. say
template <class T>
fn (T var) { ... }
Is there any way, from within the function, can we check what type of
argument we've passed on to the function (double, float etc) ??
Thanks in advance,
|
by: Suzanne Vogel |
last post by:
Is it possible to store a pointer to a template function?
eg,
template<class T> fooFunc() {...}
fptr = fooFunc; // <--
I couldn't find any info here, not even in the forums:
http://www.function-pointer.org/
|
by: firegun9 |
last post by:
Hello everyone,
here is my program:
///////////////////////////////
#include <iostream>
using namespace std;
void multi(double* arrayPtr, int len){
for(int i=0; i<len; i++)
*(arrayPtr+i)*=2;
|
by: Gurikar |
last post by:
Hello,
Can anyone tell me whats wrong in this?
// template function
template<class T>
inline void test(T* ptr)
{
cout<<"Pointer is"<<ptr<<endl;
ptr = NULL;
cout<<"Pointer is"<<ptr<<endl;
|
by: Ian |
last post by:
The title says it all.
I can see the case where a function is to be called directly from C, the
name mangling will stuff this up.
But I can't see a reason why a template function can't be given extern
"C" linkage where it is to be assigned to a C function pointer.
Ian
| |
by: Axter |
last post by:
I'm fine tuning a scope_handle class that takes a policy class as the
second template.
http://code.axter.com/scope_handle.h
Please see above link for full understanding of the problem.
One thing I don't like about the way the current policy template is
setup is that for the ptr_policy class the first template type is
different from the template type given to the policy.
And on the other policy classes, the template type is the same....
|
by: Vijai Kalyan |
last post by:
I was decomposing a task into different policies. Essentially, there is
a general option obtained from a server and user options obtained from
configuration variables. The two options are complementary to one
another.
So I proceeded to decompose the tasks that deal with user options into
two functions. Each of the functions do something and towards the end
they do supplementary tasks that depend on the server option.
The whole things...
|
by: StephQ |
last post by:
This is from a thread that I posted on another forum some days ago.
I didn't get any response, so I'm proposing it in this ng in hope of
better luck :)
The standard explanation is that pointer to functions are hard to
inline for the compiler, and so you won't be able to avoid function
call overhead.
This is an important aspect when you are calling a function very
frequently for evaluation reason: think of the problem of performing...
|
by: Clyde |
last post by:
Hi,
what i'm trying to do is:
/////////////// Code Start
template <class TType, int* p = 0>
class Template
{
public:
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |