2014-02-26 43 views
1

下面的代碼編譯沒有任何錯誤:C++ 11名decltype和數據成員

class foo 
{ 
public: 
    void some_func() {} 
}; 

class bar 
{ 
private: 
    foo instance; 

public: 
    auto some_func() -> decltype(instance.some_func()) {} 
}; 

int main() {} 

雖然以下代碼中MSVC-12.0不編譯:

class foo 
{ 
public: 
    void some_func() {} 
}; 

class bar 
{ 
private: 
    foo instance; 

public: 
    auto some_func() -> decltype(instance.some_func()); 
}; 

auto bar::some_func() -> decltype(instance.some_func()) {} 

int main() {} 

這使我的以下錯誤:

error C2228: left of '.some_func' must have class/struct/union 
error C2556: 'int bar::some_func(void)' : overloaded function differs only by return type from 'void bar::some_func(void)' 
: see declaration of 'bar::some_func' 
error C2371: 'bar::some_func' : redefinition; different basic types 
: see declaration of 'bar::some_func' 

我在做什麼錯?我該如何解決它?

+1

鐺++ 3.5和g ++ 4.8.1接受它。這是一個強烈的暗示,這是MSVC中的一個錯誤。 – dyp

+1

我認爲這是MSVC中的一個錯誤。 * trailing-return-type *不是* declarator-id *的一部分,但是在它後面。因此,名稱查找是(=應該)與* inside *成員函數主體的定義相同。 – dyp

回答

0

我現在沒有VS12,所以我有點猜測。在bar :: some_func的聲明中,當在類外聲明時,您無權訪問「實例」成員。

嘗試類似:

auto bar::some_func() -> decltype(bar::instance.some_func()) {} 
+0

不,它不起作用 – FrozenHeart

相關問題