Discussion:
simple reference "for each" question
(too old to reply)
NL
2010-02-03 21:07:11 UTC
Permalink
Hi,

A simple question, I'm sure, but I can't seem to find the answer. How
do modify the contents of a container in a for each construct? That
is, obtain a reference to the object in the container rather than a
temporary copy? (The code below _won't_ modify the "FooVector")

struct Foo {
int a;
}
vector<Foo> FooVector;

for each( Foo f in FooVector )
{
f.a = some_new_value;
}

Thanks,
NL
mzdude
2010-02-03 21:25:04 UTC
Permalink
Post by NL
Hi,
A simple question, I'm sure, but I can't seem to find the answer. How
do modify the contents of a container in a for each construct? That
is, obtain a reference to the object in the container rather than a
temporary copy? (The code below _won't_ modify the "FooVector")
struct Foo {
 int a;}
vector<Foo> FooVector;
for each( Foo f in FooVector )
{
  f.a = some_new_value;
}
try
for each(Foo &f in FooVector)
NL
2010-02-03 21:32:07 UTC
Permalink
Post by mzdude
Post by NL
Hi,
A simple question, I'm sure, but I can't seem to find the answer. How
do modify the contents of a container in a for each construct? That
is, obtain a reference to the object in the container rather than a
temporary copy? (The code below _won't_ modify the "FooVector")
struct Foo {
 int a;}
vector<Foo> FooVector;
for each( Foo f in FooVector )
{
  f.a = some_new_value;
}
try
for each(Foo &f in FooVector)
I tried that, it gives an error:

error C2440: 'static_cast' : cannot convert from 'const Foo' to 'Foo
&'
mzdude
2010-02-03 21:48:57 UTC
Permalink
Post by mzdude
Post by NL
Hi,
A simple question, I'm sure, but I can't seem to find the answer. How
do modify the contents of a container in a for each construct? That
is, obtain a reference to the object in the container rather than a
temporary copy? (The code below _won't_ modify the "FooVector")
struct Foo {
 int a;}
vector<Foo> FooVector;
for each( Foo f in FooVector )
{
  f.a = some_new_value;
}
try
for each(Foo &f in FooVector)
        error C2440: 'static_cast' : cannot convert from 'const Foo' to 'Foo
&'- Hide quoted text -
Sorry. I use the boost library and the code would look like

BOOST_FOREACH( Foo &f, FooVector )

so I thought it would be worth a shot.
NL
2010-02-04 17:59:22 UTC
Permalink
Hi,
Post by mzdude
Post by mzdude
try
for each(Foo &f in FooVector)
        error C2440: 'static_cast' : cannot convert from 'const Foo' to 'Foo
&'- Hide quoted text -
Sorry. I use the boost library and the code would look like
BOOST_FOREACH( Foo &f, FooVector )
I guess I'll switch over to the boost macro, since I'm already using
other boost stuff in the project. I was leaning toward the MS version
of for,each,in since it looks more elegant, but I guess it's limited
to "read-only".
Also the lack of thorough documentation on it leads me to think it may
be somewhat temporary.

Thanks,
Norvin
Cholo Lennon
2010-02-04 20:07:52 UTC
Permalink
Post by NL
Hi,
Post by mzdude
Post by NL
Post by mzdude
try
for each(Foo &f in FooVector)
error C2440: 'static_cast' : cannot convert from 'const Foo' to 'Foo
&'- Hide quoted text -
Sorry. I use the boost library and the code would look like
BOOST_FOREACH( Foo &f, FooVector )
I guess I'll switch over to the boost macro, since I'm already using
other boost stuff in the project. I was leaning toward the MS version
of for,each,in since it looks more elegant, but I guess it's limited
to "read-only".
Also the lack of thorough documentation on it leads me to think it may
be somewhat temporary.
You can use a const reference to avoid copies:

struct Foo {
int a;
}

vector<Foo> FooVector;

for each(const Foo& f in FooVector )
{
...
}

AFAIK using const_cast should work in this case:

for each(const Foo& f in FooVector )
{
const_cast<Foo&>(f).a = some_new_value;
}

A general solution could be:

template<typename T>
class noconst_ref_ptr
{
T* m_ptr;

public:
noconst_ref_ptr(const T& ref) : m_ptr(const_cast<T*>(&ref))
{
}

T* operator->()
{
return m_ptr;
}
};

...

for each(noconst_ref_ptr<Foo> f in FooVector )
{
f->a = some_new_value;
}


At your own risk, or use BOOSR_FOREACH, std::for_each or for :-)


Regards
--
Cholo Lennon
Bs.As.
ARG
John H.
2010-02-04 18:43:49 UTC
Permalink
How do modify the contents of a container in a for each construct?
Does the following example meet your needs?

#include <vector>
#include <algorithm>

struct Foo
{
int a;
static void Double_a(Foo & f)
{
f.a *= 2;
}
};


int main()
{
Foo const foo1 = { 1 };
Foo const foo2 = { 2 };

std::vector<Foo> fooVector;
fooVector.push_back(foo1);
fooVector.push_back(foo2);

std::for_each(fooVector.begin(), fooVector.end(), Foo::Double_a);

return 0;
}
Tim Roberts
2010-02-05 07:24:31 UTC
Permalink
Post by NL
struct Foo {
int a;
}
vector<Foo> FooVector;
for each( Foo f in FooVector )
{
f.a = some_new_value;
}
I was just about to respond that you were posting in the wrong newsgroup,
because C++ doesn't have a "for each/in" statement, when I decided I should
try it just to confirm. To my utter surprise, this compiles and even works
in VC++ 2008 with native code.

Color me surprised.
--
Tim Roberts, ***@probo.com
Providenza & Boekelheide, Inc.
Loading...