【发布时间】:2018-10-01 12:39:55
【问题描述】:
例子:
template<class T> class A{
public:
A(){}
template<class U> A(A<U> &){}
private:
template<class U> A(A<U>&&){}
};
int main() {
A<int> a1;// legal
A<int> a2(std::move(a1));//legal. it calls implicitly-declared constructor.
}
但是当我删除 A(){} 时:
template<class T> class A{
public:
template<class U> A(A<U> &){}
private:
template<class U> A(A<U>&&){}
};
int main() {
A<int> a1;// illegal. 3
A<int> a2(std::move(a1));
}
- 如果模板构造函数不影响隐式声明的规则。为什么它变得非法?
- 如果模板构造函数确实影响隐式声明的规则,为什么
A<int> a2(std::move(a1));在第一个示例中不是非法的?
在 gcc 和 ubuntu 上测试过。
【问题讨论】:
-
谁告诉你模板构造函数不会影响隐式声明规则?
-
但是为什么“A
a2(std::move(a1));”合法的? @NathanOliver -
@Long on MSVC
A<int> a2(std::move(a1));也是非法的。 -
@Rhathin 你用的是什么版本?它适用于我的答案细节,它应该。
-
@Rhathin 你真的应该更新。版本 11 (MSVS 2012) 的 C++11 合规性很差。 MSVS 2017 具有更好的支持和合规性。 (我认为现在甚至可能是完全合规和支持)
标签: c++ c++11 templates constructor