CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 13 of 13
  1. #1
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    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 ?

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: is "result" a reserved name ?

    Seems like an easy way to find out would be to call it something else and try again.

  3. #3
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    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...

  4. #4
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    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 ?

  5. #5
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: is "result" a reserved name ?

    According to the C++ Keywords result is not one.
    Best regards,
    Igor

  6. #6
    Join Date
    Oct 2008
    Posts
    1,456

    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 ...

  7. #7
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    Re: is "result" a reserved name ?

    Quote Originally Posted by superbonzo View Post
    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();
    }

  8. #8
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    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.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  9. #9
    Join Date
    Oct 2008
    Posts
    1,456

    Re: is "result" a reserved name ?

    Quote Originally Posted by Philip Nicoletti View Post
    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.

  10. #10
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,725

    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

  11. #11
    Join Date
    Oct 2008
    Posts
    1,456

    Re: is "result" a reserved name ?

    Quote Originally Posted by Philip Nicoletti View Post
    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 ...

  12. #12
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    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.

  13. #13
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: is "result" a reserved name ?

    I've tested with VS2012 RC and is fixed indeed.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured