C++ 可变模板和通过赋值复制构造

C++ 可变模板和通过赋值复制构造,c++,c++11,C++,C++11,考虑这个最小的例子: template <typename T, typename U> struct foo {}; template <template <typename...> class Bar> struct converter { template <typename... Args> converter(const Bar<Args...> &); }; int main() {

考虑这个最小的例子:

template <typename T, typename U>
struct foo {};

template <template <typename...> class Bar>
struct converter
{
     template <typename... Args>
     converter(const Bar<Args...> &);
};

int main()
{
    converter<foo> c(foo<int,double>()); // This works.
    // converter<foo> c = foo<int,double>(); This fails
}
模板
结构foo{};
模板
结构转换器
{
模板
转换器(常数棒和);
};
int main()
{
转换器c(foo());//这是可行的。
//转换器c=foo();此操作失败
}
注释掉的行在GCC 4.5和4.6中都失败,消息如下:

main.cpp:10:2: error: wrong number of template arguments (1, should be 2)
main.cpp:4:8: error: provided for template<class T, class U> struct foo
main.cpp: In function int main():
main.cpp:15:37: error: conversion from foo<int, double> to non-scalar type converter<foo> requested
main.cpp:10:2:错误:模板参数的数量错误(1,应该是2)
main.cpp:4:8:错误:为模板结构foo提供
main.cpp:在函数int main()中:
main.cpp:15:37:错误:请求从foo转换为非标量类型转换器

如果不使用可变模板,而是使用特定数量的模板参数(即,在这种情况下为2),则不存在错误。我有点困惑,因为我希望这两行完全相同:这是一种预期的行为吗?

是的,这应该是有效的。这是一个GCC错误。GCC还不完全支持C++0x可变模板(公平地说,规范的细节仍在不断变化)

你所说的“这很有效”实际上是在声明一个函数;它不会初始化一个对象,这正是您想要的


有关您想要了解的内容,请参见14.3.3p3,其中描述了
模板类栏如何匹配
foo
,以及14.8.2.5p9,其中描述了
foo
如何匹配
foo

是的,这应该是有效的。这是一个GCC错误。GCC还不完全支持C++0x可变模板(公平地说,规范的细节仍在不断变化)

你所说的“这很有效”实际上是在声明一个函数;它不会初始化一个对象,这正是您想要的

有关您想要了解的内容,请参见14.3.3p3,其中描述了
模板类栏如何匹配
foo
,以及14.8.2.5p9,其中描述了
foo
如何匹配
foo

模板
结构foo{};
结构转换器
{
模板
转换器(常数条&){}
};
int main()
{
转换器c1((foo());//这是可行的。
converter c2=foo();//这是可行的
}
模板
结构foo{};
结构转换器
{
模板
转换器(常数条&){}
};
int main()
{
转换器c1((foo());//这是可行的。
converter c2=foo();//这是可行的
}

c=converter(foo());工作?不,正如Johannes在下面指出的,我错误地认为main()的第一行实际上初始化了任何东西。您的建议会产生与注释行相同的错误;工作?不,正如Johannes在下面指出的,我错误地认为main()的第一行实际上初始化了任何东西。您的建议会产生与注释行相同的错误。Doh,我的错。。当然,你没有实际初始化任何东西是对的。谢谢你的指点:)哦,我的坏。。当然,你没有实际初始化任何东西是对的。谢谢你的指点:)谢谢,这确实解决了这个问题。。。不幸的是,它不能很容易地转化为我在课堂上的实际用法:(谢谢,这确实解决了这个问题……不幸的是,它不能很容易地转化为我在课堂上的实际用法:(
template <typename T, typename U>
struct foo {};


struct converter
{
 template <template <typename...> class Bar, class ...Args>
     converter(const Bar<Args...> &){}
};

int main()
{
    converter c1((foo<int,double>())); // This works.
    converter c2 = foo<int,double>();// This works
}