C++ (默认)为每个变量类型构造一个对象

C++ (默认)为每个变量类型构造一个对象,c++,visual-c++,c++11,variadic-templates,compiler-bug,C++,Visual C++,C++11,Variadic Templates,Compiler Bug,考虑以下代码片段: void Foo(std::string str1, std::string str2) {} template<typename... Types> void Bar() { Foo(Types{}...); // wont compile } Bar<std::string, std::string>(); void Foo(std::string str1,std::string str2){ 模板 空条() { Foo(类型{}…

考虑以下代码片段:

void Foo(std::string str1, std::string str2) {}

template<typename... Types>
void Bar()
{
    Foo(Types{}...); // wont compile
}

Bar<std::string, std::string>();
void Foo(std::string str1,std::string str2){
模板
空条()
{
Foo(类型{}…;//不会编译
}
Bar();
这里我要做的是在
Bar
方法中默认构造两个
std::string
对象,并将它们传递给
Foo
。然而,我徒劳的尝试(其中一个在代码段中)无法编译,所以我想知道这是否可能


我用VC 2013编译,这会给我带来编译器错误。如注释中所述,其他编译器可以处理它。有人能告诉我上面的代码片段是否符合标准吗?

这会使我的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.
1>main.cpp(23):错误C2144:语法错误:“std::string”前面应加“')”
1> cpp(28):参见正在编译的函数模板实例化“void Bar(void)”的参考
1> main.cpp(23):错误C2660:“Foo”:函数不接受0个参数
1> main.cpp(23):错误C2143:语法错误:缺少“;”在“{”之前
1> main.cpp(23):错误C2143:语法错误:缺少“;”before“,”
1> c1xx:致命错误C1063:内部编译器错误
1 >请选择Visual C++上的技术支持命令
1> 帮助菜单,或打开技术支持帮助文件以了解更多信息
1> cl:命令行警告D9028:最小重建失败,恢复到正常生成
1>
1> 生成失败。

这是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>();
}
使用标识的模板\u t=t;//新代码
无效Foo(int,int);
模板
空条()
{
Foo(identity_t{}…)//使用identity类型转换
}
int main(){
Bar();
}

我还没有找到发行号。

@0x499602D2有趣的是,用VC 2013尝试了你的代码,但失败了(正如我之前观察到的那样),可能是一个编译器错误,然后也在clang 3.5上工作,你的编译器和版本是什么?
模板使用identity\u t=t;
也可以工作,不需要
typename
::type
装饰。@Manu343726不太可能,考虑到他们还没有完成C++98功能的实现,而且他们已经有16年的时间来处理它按标准修订。