|
-
August 7th, 2012, 08:07 AM
#1
is "result" a reserved name ?
Code:
class foo
{
int wesult;
int result;
template<class Pred>
void call(Pred pred)
{
Pred();
}
void test()
{
call([&](){ wesult=1; result=1; });
};
};
In the lamda function, I am apparently allowed to access/change a variable named wesult, but when I have a variable named result, I get a weird error:
Code:
1>c:\test\test.cpp(19): error C2326: 'void `anonymous-namespace'::<lambda0>::operator ()(void) const' : function cannot access 'foo::result'
1>c:\test\test.cpp(19): error C2166: l-value specifies const object
This is with VS2010.
Is 'result' a reserved keyword when used in combination with lambda's ? or is this some weird error ?
If I remove/comment out the result=1; then it compiles and runs normally.
Can anyone give this a try in VS2012 ?
-
August 7th, 2012, 08:24 AM
#2
Re: is "result" a reserved name ?
Seems like an easy way to find out would be to call it something else and try again.
-
August 7th, 2012, 08:55 AM
#3
Re: is "result" a reserved name ?
But he did - called it "wesult" and it worked...
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
August 8th, 2012, 02:59 AM
#4
Re: is "result" a reserved name ?
indeed. it works if I rename "result" to anything I tried so far. So is this a bug, or is "result" something special in this case ? A new/planned keyword for which (partial ?) support is present already ?
-
August 8th, 2012, 04:43 AM
#5
Re: is "result" a reserved name ?
According to the C++ Keywords result is not one.
Best regards,
Igor
-
August 8th, 2012, 07:38 AM
#6
Re: is "result" a reserved name ?
in VS2010, with the exact code you posted I cannot reproduce the problem. It just gives me an error saying that Pred has no default ctor, as expected. Is that a typo ? anyway, replacing "Pred()" with "pred()" as it's probably meant to be, it just compiles fine ...
-
August 8th, 2012, 08:42 AM
#7
Re: is "result" a reserved name ?
 Originally Posted by superbonzo
in VS2010, with the exact code you posted I cannot reproduce the problem. It just gives me an error saying that Pred has no default ctor, as expected. Is that a typo ? anyway, replacing "Pred()" with "pred()" as it's probably meant to be, it just compiles fine ... :confused:
I think that you actually need to use to function to generate the
compile error (after making test() public).
Code:
int main()
{
foo fobj;
fobj.test();
}
-
August 8th, 2012, 08:57 AM
#8
Re: is "result" a reserved name ?
Reproduced with VS2010. No problem with VS 2012 RC.
However, I get a different result:
1>vs2010_win32_test.cpp(54): error C2326: 'void `anonymous-namespace'::<lambda0>:  perator ()(void) const' : function cannot access 'foo::result'
1>vs2010_win32_test.cpp(54): error C3491: '__this': a by-value capture cannot be modified in a non-mutable lambda
I guess C3491 is key here, because the following works correctly:
Code:
void test()
{
call([=](){ this->result=1; });
};
Notice that in VS2012 that is not necessary to specify = for capturing this by value, since this is always captured by value.
-
August 8th, 2012, 09:36 AM
#9
Re: is "result" a reserved name ?
 Originally Posted by Philip Nicoletti
I think that you actually need to use to function to generate the
compile error (after making test() public).
no, foo is not a class template and test is an ordinary member function. The compiler is required to instantiate the call<> template when parsing the test() body, so no need to "use" the function. In any case, in my vs2010 copy the code:
Code:
class foo
{
int wesult;
int result;
template<class Pred>
void call(Pred pred)
{
pred();
}
public:
void test()
{
call([&]{ wesult=1; result=1; });
};
};
int main()
{
foo fobj;
fobj.test();
}
compiles fine as well.
-
August 8th, 2012, 11:33 AM
#10
Re: is "result" a reserved name ?
Strange ... the code you posted also compiles on my system.
My code was identical to yours, except I had an
Code:
#include <iostream>
at the top, which causes the compile error:
[quote]
1>Source1.cpp(16): error C2326: 'void `anonymous-namespace'::<lambda0>::operator ()(void) const' : function cannot access 'foo::result'
1>Source1.cpp(16): error C2166: l-value specifies const object
[/code]
16 .. is the "result = 1;" line
-
August 8th, 2012, 11:58 AM
#11
Re: is "result" a reserved name ?
 Originally Posted by Philip Nicoletti
Strange ... the code you posted also compiles on my system. My code was identical to yours, except I had an
ok, for the sake of curiosity, I've traced back the problem into iostream; it's surely a compiler bug involving name lokup:
Code:
typedef int whatever;
class foo
{
int whatever;
template<class Pred>
void call(Pred pred);
void test() { call([this]{ whatever = 1; }); }
};
the above reproduces the error as well; note that if you place the typedef inside a struct you still get the same result ( this is what happens deep inside <iostream> ), in no way this can be legal. BTW, yet another workaround is to write foo::whatever ...
-
August 9th, 2012, 06:59 AM
#12
Re: is "result" a reserved name ?
Took some digging further into this matter via Microsoft, and it is confirmed to be a name lookup issue in VS2010. The issue is apparently (but I can't test this atm) resolved in VS2012.
-
August 9th, 2012, 07:23 AM
#13
Re: is "result" a reserved name ?
I've tested with VS2012 RC and is fixed indeed.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|