2012-06-20 37 views
3

我正在使用MSVC10。無法在命名嵌套類時在Lambda中解析名稱

我有一個嵌套在類B中的類C,它又嵌套在類A中。 B具有C類型的成員變量,而A具有vectorB s。像這樣:

class A 
{ 
    class B 
    { 
    string foo_; 
    class C 
    { 
     string bar_; 
    } c_; 
    }; 
    vector<B> b_; 
}; 

A我有一個使用for_each與拉姆達的成員函數,遍歷vector<B>

在這拉姆達我嘗試獲取到B基準和C(分別):

void A::Run() 
{ 
    for_each(b_.begin(), b_.end(), [](std::vector<B>::value_type& that) 
    { 
     const B& b = that; 
     cout << b.foo_; 
     const B::C& c = b.c_; // 'B' : is not a class or namespace name 
     // const A::B::C& c = b.c_; <-- THIS COMPILES 
     cout << c.bar_; 
    }); 
} 

的代碼:const B::C& c = b.c_;導致編譯器錯誤,「‘B’:不是類或命名空間名稱「即使編譯器沒有問題接受const B& b = that;

語言是否允許此語法?

如果將其更改爲:const A::B::C& c = b.c_;編譯器接受它。

下面是一個完整的例子給你玩:

#include <string> 
#include <iostream> 
#include <vector> 
#include <algorithm> 
using namespace std; 

void foo() {} 

class A 
{ 
public: 
    void Run(); 

    struct B 
    { 
     std::string foo_; 
     struct C 
     { 
      std::string bar_; 
     } c_; 
    }; 

    std::vector<B> b_; 
}; 

void A::Run() 
{ 
    for_each(b_.begin(), b_.end(), [](std::vector<B>::value_type& that) 
    { 
     const B& b = that; 
     cout << b.foo_; 
     const B::C& c = b.c_; // 'B' : is not a class or namespace name 
     // const A::B::C& c = b.c_; <-- THIS COMPILES 
     cout << c.bar_; 
    }); 
} 

int main() 
{ 
    A a; 
    a.Run(); 
} 
+0

這是什麼編譯器? –

+0

對不起,這是MSVC10。我會更新這個問題。 –

回答

2

它在編譯器中的錯誤。該代碼與MSVC 2012 RC編譯良好。我相信相關的錯誤是this one

而標準的相關部分是[expr.prim.lambda] 5.1.2第7:

的λ-表達的化合物語句產生的功能的功能體 (8.4) (3.4.2),確定這個(9.3.2)的類型和值以及將非靜態類成員引用到類成員中的 訪問表達式使用(*)調用操作符,但是用於名稱查找 這個)(9.3.1),複合陳述是 認爲是在lambda表達式的上下文中。

+0

+1,我無法用臭蟲報告鏈接努力地提供答案。 – ildjarn

相關問題