【发布时间】:2016-03-26 05:04:01
【问题描述】:
更新:
谢谢你,Jamboree。
这是最后的struct A。
struct A
{
template<class ... Args,class=std::enable_if_t<(sizeof...(Args)!=1)>>
A(Args &&...args)
{
cout<<'v';
}
template<class Arg,class=std::enable_if_t<!std::is_base_of<std::remove_reference_t<Arg>,A>::value>>
A(Arg &&arg)
{
cout<<'v';
}
A(const A &)
{
cout<<'c';
}
A(A &&)
{
cout<<'m';
}
};
产地:
关于这段代码,
#include<iostream>
#include<type_traits>
#include<utility>
using namespace std;
struct A
{
template<class ... Args,class=std::enable_if_t<
sizeof...(Args)!=1
||!std::is_same<std::remove_cv_t<std::remove_reference_t<Args>>,A>::value>>
A(Args &&...args)
{
cout<<'v';
}
A(const A &)
{
cout<<'c';
}
A(A &&)
{
cout<<'m';
}
};
int main()
{
A a{10};
A b{10,20};
A c{b};
const A d{c};
A e{move(c)};
}
在 VC++ 14.0 中输出为vvvvm。
但是为什么输出不是vvccm?
(我希望c and d 使用复制构造函数。而且我知道 Effective Modern C++ Item 27 只使用一个转发引用。)
【问题讨论】:
-
使用
Argsunexpanded 应该是格式错误的。 -
但是如果我使用
std::remove_reference_t<Args...>,就会出现编译错误。
标签: c++ templates c++14 sfinae variadic