2016-01-04 48 views
3

有什麼辦法有XCode中顯示C++的變量類型快速幫助?目前,當選擇一個變量時,它只顯示「Declared int foo.cc」。顯示在Xcode的「快速幫助」變量類型

在AppCode這個作品,是「自動」變量特別有用,因爲它顯示了扣除變量類型。

它可以正確處理方法。

目前使用的XCode 7.2。

回答

1

我不認爲這是可能的了XCode 7.2(或更低)

到目前爲止我能找到最接近的是強制的XCode向您展示一個錯誤。例如。假設你有這樣的代碼:

template<typename K, typename V> 
std::map<K, V> createMap() 
{ 
    return std::map<K, V>(); 
} 

void process() 
{ 
    auto myMap = createMap<std::string, int>(); 
    // ... 
} 

你需要找出什麼是myMap變量的類型。

創建輔助類和宏:

template <typename T> class DeductTypeCatcher { 
public: 
    DeductTypeCatcher() { T t = (void***)0; } 
}; 

#define SHOW_DEDUCT_TYPE(t)  DeductTypeCatcher<decltype(t)> __catcher__; 

然後添加到您的原代碼:

void process() 
{ 
    auto myMap = createMap<std::string, int>(); 
    SHOW_DEDUCT_TYPE(myMap) 
} 

和XCode中會立即告訴你一個錯誤

No viable conversion from 'void ***' to 'std::__1::map<std::__1::basic_string<char>, int, std::__1::less<std::__1::basic_string<char> >, std::__1::allocator<std::__1::pair<const std::__1::basic_string<char>, int> > >' 

或假設你有這樣的代碼:

void process() 
{ 
    auto myMap = createMap<std::string, int>(); 
    for (auto it : myMap) { /*...*/ } 
} 

,並希望找出 「它」 的類型:

void process() 
{ 
    auto myMap = createMap<std::string, int>(); 
    SHOW_DEDUCT_TYPE(*myMap.begin()) 
    for (auto it : myMap) { /*...*/ } 
} 

會告訴你一個錯誤

Non-const lvalue reference to type 'std::__1::pair<const std::__1::basic_string<char>, int>' cannot bind to a temporary of type 'void ***' 

我知道這是不是一個完美的解決方案,但至少比沒有好。

編輯:

另一種方法可能是依賴於IDE本身。

E.g.在此示例中:

void process() 
{ 
    auto myMap = createMap<std::string, int>(); 
    SHOW_DEDUCT_TYPE(*myMap.begin()) 
    for (auto it : myMap) { 
    } 
} 

開始在for循環中鍵入「it」(不帶引號)。自動完成對話框將彈出,您應該在自動填充框的左側看到「it」變量的類型。如果類型太長然後鼠標懸停在它,等待幾秒鐘 - 完整的類型將顯示(有時是豐滿型的顯示不出來,那麼你必須按下Ctrl + Space幾次)。

enter image description here

enter image description here

+0

感謝。我的快速幫助意圖是快速查看變量類型。是的,我還發現,自動完成對話框(Ctrl +空格鍵)顯示類型,但在快速幫助查看它會更快。 – amfcosta