Are function-local typedefs visible inside C++0x lambdas?

后端 未结 4 963
既然无缘
既然无缘 2021-01-11 13:27

I\'ve run into a strange problem. The following simplified code reproduces the problem in MSVC 2010:

template 
struct dummy
{
    static T f         


        
相关标签:
4条回答
  • 2021-01-11 13:59

    From n3000, 5.1.2/6,

    The lambda-expression’s compound-statement yields the function-body (8.4) of the function call operator, but for purposes of name lookup (3.4), … the compound-statement is considered in the context of the lambda-expression.

    Not surprisingly, the local type should be visible.

    0 讨论(0)
  • 2021-01-11 14:01

    I have filed two bug reports.

    • The crash bug by Tarydon. (Report)
    • The lambda scope resolution bug. (Report)

    We'll see how it goes. :)


    Update

    Both bugs have been marked as fixed:

    We appreciate your feedback. This bug has been seen by us before and we have fixed it in next release. Thank you for using the product.

    Thanks,
    Ulzii Luvsanbat
    Windows C++ Team

    So there we go.

    0 讨论(0)
  • 2021-01-11 14:10

    This is not really an answer to your question, but just exploring the problem further. I was wondering if the compiler has issues dealing with types declared in an enclosing scope, so tried this out:

    #include <iostream>
    
    template <typename Func>
    void do_test(Func pFunc) {
    }
    
    template <typename T>
    void test_trait(void) {
       class Something { public: int foo; };
    
       do_test ([] (T pX) {
          Something A; A.foo = 12;
       });
    }
    
    int main(void) {
        test_trait<int> ();
    }
    

    Here, I'm just trying to create a local type in the enclosing scope and use it from within the lambda function. Not only does this not compile (with Visual Studio 2010, Beta 2) but it actually crashes the compiler with a C1001 internal error.

    0 讨论(0)
  • 2021-01-11 14:13

    Function-local enums cannot be detected by lambdas either.

    int main()
    {   
        enum E {A, B, C};   
        auto x = [](){ int a = A; }; 
        //auto y = [](){ E a = A; }; // this will crash the compiler
    }
    

    error C3493: 'A' cannot be implicitly captured because no default capture mode has been specified

    Following is a workround, problematic-maybe though.

    int main()
    {   
        enum E {A, B, C};   
        auto x = [=](){ int a = A; };
        // typedef E F; 
        // auto y = [=](){ F a = A; }; // this compiles ok
    }
    
    0 讨论(0)
提交回复
热议问题