[foreach] BOOST_FOREACH(x, *this)

Hi gurus, VC++7.1/8 complains: error C2355: 'this' : can only be referenced inside non-static member about: struct xxx : std::vector<int> { void test() { BOOST_FOREACH(int x, *this) { } } }; GCC compiles. Is this maybe a bug of VC++? -- Shunsuke Sogame

Shunsuke Sogame wrote:
Hi gurus,
VC++7.1/8 complains:
error C2355: 'this' : can only be referenced inside non-static member
about:
struct xxx : std::vector<int> { void test() { BOOST_FOREACH(int x, *this) { } } };
GCC compiles. Is this maybe a bug of VC++?
Looks like it. I can't think of a work-around. :-( -- Eric Niebler Boost Consulting www.boost-consulting.com

Eric Niebler wrote:
Shunsuke Sogame wrote:
Hi gurus,
VC++7.1/8 complains:
error C2355: 'this' : can only be referenced inside non-static member
about:
struct xxx : std::vector<int> { void test() { BOOST_FOREACH(int x, *this) { } } };
GCC compiles. Is this maybe a bug of VC++?
Looks like it. I can't think of a work-around. :-(
I just wanted to know whether it is legal or a bug. That is somewhat problematic if 'xxx' doesn't know the base range type. I note user workaround: struct xxx : std::vector<int> { void test() { foreach(*this); } template< class SelfT > void foreach(SelfT& self) { BOOST_FOREACH (int x, self) { } } }; Thanks! -- Shunsuke Sogame

Shunsuke Sogame ha scritto:
I note user workaround:
struct xxx : std::vector<int> { void test() { foreach(*this); }
template< class SelfT > void foreach(SelfT& self) { BOOST_FOREACH (int x, self) { } } };
A much simpler workaround that compiles under VC7.1 is: struct xxx : std::vector<int> { void test() { xxx* This = this; BOOST_FOREACH(int x, *This) { } } }; HTH, Ganesh

Alberto Ganesh Barbati wrote:
Shunsuke Sogame ha scritto:
I note user workaround:
struct xxx : std::vector<int> { void test() { foreach(*this); }
template< class SelfT > void foreach(SelfT& self) { BOOST_FOREACH (int x, self) { } } };
A much simpler workaround that compiles under VC7.1 is:
struct xxx : std::vector<int> { void test() { xxx* This = this;
BOOST_FOREACH(int x, *This) { } } };
Doh! A mysterious reason prevented me from writing even self type... Bug makes bug. :-) Thanks again! -- Shunsuke Sogame

"Eric Niebler" <eric@boost-consulting.com> wrote in message news:44524BF1.9070801@boost-consulting.com...
Shunsuke Sogame wrote:
Hi gurus,
VC++7.1/8 complains:
error C2355: 'this' : can only be referenced inside non-static member
about:
struct xxx : std::vector<int> { void test() { BOOST_FOREACH(int x, *this) { } } };
GCC compiles. Is this maybe a bug of VC++?
Looks like it. I can't think of a work-around. :-(
What happens if you define test() out of line? Do you still get the error? Joe Gottman

"Eric Niebler" <eric@boost-consulting.com> wrote in message news:44524BF1.9070801@boost-consulting.com...
Shunsuke Sogame wrote:
Hi gurus,
VC++7.1/8 complains:
error C2355: 'this' : can only be referenced inside non-static member
about:
struct xxx : std::vector<int> { void test() { BOOST_FOREACH(int x, *this) { } } };
GCC compiles. Is this maybe a bug of VC++?
Looks like it. I can't think of a work-around. :-(
What happens if you define test() out of line? Do you still get the error? Joe Gottman
participants (4)
-
Alberto Ganesh Barbati
-
Eric Niebler
-
Joe Gottman
-
Shunsuke Sogame