[英](Default) construct an object for every variadic type
考慮以下代碼片段:
void Foo(std::string str1, std::string str2) {}
template<typename... Types>
void Bar()
{
Foo(Types{}...); // wont compile
}
Bar<std::string, std::string>();
我在這里要做的是默認在Bar
方法內構造兩個std::string
對象,並將它們傳遞給Foo
。 但是我的徒勞嘗試(其中之一在摘要中)無法編譯,因此我想知道這是否有可能。
我使用VC 2013進行編譯,這使我感到編譯器錯誤。 如評論中所述,其他編譯器可以處理它。 誰能說出上述片段是否符合標准?
這是MSVC可變參數模板擴展過程中的一個問題。 當解壓縮類型列表時,它無法識別出它們適合於構造函數調用。 解決方法是,可以執行類型轉換以強制編譯器識別它們:
template<typename T> using identity_t = T; // NEW CODE
void Foo(int, int);
template<typename... Types>
void Bar()
{
Foo(identity_t<Types>{}...); // use identity type transformation
}
int main() {
Bar<int, int>();
}
我尚未找到問題編號。
這會使VC 2013編譯器崩潰。 該錯誤似乎表明它在解析代碼時遇到了一些問題。 因此,當編譯器崩潰時,它一定是編譯器錯誤。
1>main.cpp(23): error C2144: syntax error : 'std::string' should be preceded by ')'
1> main.cpp(28) : see reference to function template instantiation 'void Bar<std::string,std::string>(void)' being compiled
1>main.cpp(23): error C2660: 'Foo' : function does not take 0 arguments
1>main.cpp(23): error C2143: syntax error : missing ';' before '{'
1>main.cpp(23): error C2143: syntax error : missing ';' before ','
1>c1xx : fatal error C1063: INTERNAL COMPILER ERROR
1> Please choose the Technical Support command on the Visual C++
1> Help menu, or open the Technical Support help file for more information
1>cl : Command line warning D9028: minimal rebuild failure, reverting to normal build
1>
1>Build FAILED.
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.