2013-11-28 61 views
40

這constexpr代碼編譯沒有沒有在Visual Studio 2013版本編譯12.0.21005.1 RELconstexpr在VC2013

是否有新的Visual Studio編譯器對使用constexpr的作品?從編譯

#include <iostream> 

constexpr int factorial(int n) 
{ 
    return n <= 1 ? 1 : (n * factorial(n - 1)); 
} 

int main(void) 
{ 
    const int fact_three = factorial(3); 
    std::cout << fact_three << std::endl; 
    return 0; 
} 

輸出:

 1>------ Build started: Project: Project1, Configuration: Debug Win32 ------ 
    1> Source.cpp 
    1>....\source.cpp(3): error C2144: syntax error : 'int' should be preceded by ';' 
    1>....\source.cpp(3): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

香草薩特提到在他的博客constexpr,但目前還不清楚它的工作原理是什麼版本/是否行得通呢? http://herbsutter.com/2013/09/09/visual-studio-2013-rc-is-now-available/#comment-13521

+10

你需要讓[2013 CTP(http://blogs.msdn.com/b/vcblog/archive /2013/11/18/announcing-the-visual-c-compiler-november-2013-ctp.aspx),儘管注意它仍然不完整(尚未爲成員函數實現))。 –

+1

編譯並運行良好的gcc版本4.8.2(GCC)爲cygwin,--std = C++ 11 – RichardPlunkett

+2

是2013 CTP的工作就像一個魅力! ;-) – Damian

回答

45

Microsoft發佈了一個C++ 11兼容性表,其中constexprclearly marked as not being available in Visual Studio 2013

The November 2013 CTP有,但。

來源:谷歌visual studio constexpr

+0

我有VS 2k13更新3,但不是這個編譯器。我必須安裝它。你知道他們是否會用更新的更新發布它,或者我們必須等到vs14? – blfuentes

+0

@blacai:我不知道。爲什麼不嘗試2013年11月的CTP? –

+0

這就是我所做的。但我正在檢查是否計劃在主要更新中更新編譯器。 nov13 ctp去年發佈,vs2k13在2014年有幾個更新 – blfuentes

9

constexpr在Visual Studio 2013 RTM中不受支持,請參閱compatibility table。這不僅適用於RTM版本,還適用於Visual Studio更新。

如果您想堅持使用Visual Studio 2013,您可以下載帶有一些新功能的Visual C++ Compiler November 2013 CTP,請參閱MSDN blog。不幸的是,微軟沒有與最新的Visual Studio更新功能和CTP功能合併,並明確表示他們不打算這樣做。

如果我們想要這一切,我們需要等待Visual Studio 2015,請參閱MSDN blog about VS 2015 Preview

+0

「它仍然不受支持」在此CTP中,檢查來自上面的'軌道上的亮度比賽'的評論... –

+0

@讓·戴維:對不起,但這是無稽之談。我只是用鏈接的CTP編譯問題的代碼。 –

+0

如果您要更新答案,現在應提供VS2015預覽版的任何參考資料/指向CTP的鏈接。 – ildjarn

3

由於是由其他人所說,November 2013 Customer Technology Preview(CTP)會給你訪問constexpr *

注意,僅下載你需要改變你的「平臺工具集「轉換爲」Visual C++ Compiler Nov 2013 CTP(CTP_Nov2013)「以使用新編譯器。你可以通過打開你的項目的「Property Pages」來做到這一點,然後轉到:「Configuration Properties」>「General」,然後更改「Platform Toolset」。

*你實際上有權訪問constexpr的哪部分信息有點矛盾,但絕對不是constexpr的全部標準定義。微軟稱here了2013年11月CTP補充說:

constexpr支持(除了構造函數)

微軟說here它包含:

constexpr(除成員函數)

我甚至不能測試它是否支持端口爲會員功能,因此它絕對不支持任何類型的constexpr建設。例如this code給出了這樣的錯誤與2013年11月CTP:

錯誤C2127:與非常量表達式「constexpr」實體的非法初始化

另外也請注意:在編寫Visual的時間Studio 2015 Preview仍不支持constexpr構建。在最終版本中保持我的手指交叉。