I have an STL container of functors and I want to execute them using
std::for_each. In the code snippet below the line marked "// good"
works fine and the one marked "// nothing" compiles fine but does
absolutely nothing. Can anyone explain why? I'm using gcc 4.1.1 on
Solaris8.
Thanks,
Marius
#include <iostream>
#include <deque>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
using namespace std;
using namespace boost::lambda;
template <typename C>
void call_all(C& c)
{
std::for_each(c .begin(), c.end(), _1);
// nothing
std::for_each(c .begin(), c.end(), bind(&C::value_ type::operator( ),
_1)); // good
}
struct Func
{
Func(int s) : i(s) {}
void operator()() const { cerr << __PRETTY_FUNCTI ON__ << " " << i <<
endl; }
int i;
};
int
main()
{
deque<Funcdeq;
deq.push_back(F unc(1));
deq.push_back(F unc(2));
deq.push_back(F unc(3));
call_all(deq);
return 0;
} 5 2286
marius lazer wrote:
I have an STL container of functors and I want to execute them using
std::for_each. In the code snippet below the line marked "// good"
works fine and the one marked "// nothing" compiles fine but does
absolutely nothing. Can anyone explain why? I'm using gcc 4.1.1 on
Solaris8.
You might consider the boost mailing list as a better place to ask.
marius lazer wrote:
works fine and the one marked "// nothing" compiles fine but does
absolutely nothing. Can anyone explain why?
Because the lambda expression _1 is the identity function.
for each item in collection do identity
is a useless noop.
Kaz Kylheku wrote:
Because the lambda expression _1 is the identity function.
for each item in collection do identity
is a useless noop.
Maybe I misunderstand std::for_each, but I thought it's supposed to
call its third argument for each element in the container and the
elements are functors in this case. So yes, _1 is identity but it's not
being called. That's the piece I don't understand...
Thanks,
Marius
marius lazer wrote:
Kaz Kylheku wrote:
>Because the lambda expression _1 is the identity function.
for each item in collection do identity
is a useless noop.
Maybe I misunderstand std::for_each, but I thought it's supposed to
call its third argument for each element in the container and the
elements are functors in this case. So yes, _1 is identity but it's not
being called. That's the piece I don't understand...
Yes it is being called; it just does nothing. In the following, Foo()
and Bar() are equivalent:
struct Dummy { void operator()() const {} };
void Foo()
{
std::foreach(c. begin(), c.end(), _1);
}
void Bar()
{
std::foreach(c. begin(), c.end(), Dummy());
}
--
Clark S. Cox III cl*******@gmail .com
marius lazer wrote:
Kaz Kylheku wrote:
Because the lambda expression _1 is the identity function.
for each item in collection do identity
is a useless noop.
Maybe I misunderstand std::for_each, but I thought it's supposed to
call its third argument for each element in the container and the
elements are functors in this case. So yes, _1 is identity but it's not
How is it relevant that the elements are functors? They aren't going to
be called. They are just passed to the identity function which returns
them, and the return value is discarded. So these elements could just
as well be integers or strings.
being called. That's the piece I don't understand...
What side effects does _1 produce by which you can tell whether or not
it is being called?
Assuming that _1 is not called, how would the behavior be different if
_1 /was/ called?
Would the compiler be wrong in deducing that _1 doesn't actually have
to be called? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Rafal Dabrowa |
last post by:
Suppose I have the following structure:
struct xy { int x, y; };
and the following vector:
vector<xy> v(10);
I want to use boost/lambda for find an item in the table.
Let's say, with x member value equal to 10. I wrote
|
by: ogerchikov |
last post by:
Hi,
I am reading a Boost.lambda example
http://www.awprofessional.com/articles/article.asp?p=400651&seqNum=4.
Here is the example:
void demoVector() {
vector<int> myvector;
myvector.push_back(1);
|
by: Toby Bradshaw |
last post by:
Hi,
Consider the following:
class A
{
public:
virtual bool foo() = 0;
};
|
by: flopbucket |
last post by:
Hi,
After reading a bit about boost::lambda, I became curious how they
implemented it. I downloaded it and had a look, but the all the
headers and multiple templates make it a bit difficult to follow in a
short time (I only spent maybe 15 minutes).
Anyway, I decided to try some ideas out and came up with the following
basic example: (obviously this is very far from boost::lambda and is
special for streams, etc., but just trying to...
|
by: silverburgh.meryl |
last post by:
I have code which uses Boost lambda in a template like this:
using namespace boost::lambda;
template<class T>
bool lessThanXY( T& src, T& dest ) {
return (src.getY() < dest.getY());
}
template<class T1, class T2>
| |
by: rammel |
last post by:
hi,
can't I call member functions from an placeholder of a boost-like
lambda expression?
look on the example below please:
-----------------------------------------------------------
#include <iostream>
#include <algorithm>
#include <iterator>
|
by: nandor.sieben |
last post by:
Let m be of type vector<vector<int. I am trying to sort each
element of m:
for_each(m.begin(), m.end(), bind(sort, *_1.begin(), *_1.end());
This does not work:
'const struct
boost::lambda::lambda_functor<boost::lambda::placeholder<1' has no
member named 'begin'
|
by: Tim H |
last post by:
Compiling with g++ 4:
This line:
if_then_else_return(_1 == 0, 64, _1)
When called with a bignum class as an argument yields:
/usr/include/boost/lambda/if.hpp: In member function 'RET
boost::lambda::lambda_
functor_base<boost::lambda::other_action<boost::lambda::ifthenelsereturn_action>
|
by: Stuart Golodetz |
last post by:
Hi,
Just wondering why the following code doesn't work:
#include <iostream>
#include <boost/lambda/lambda.hpp>
using namespace boost::lambda;
int main()
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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: 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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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
| |