Wha*_*rld 6 c++ templates c++11
我有一个人类使用通用参考ctor
class Human {
public:
template<typename T>
explicit Human(T&& rhs) {
// do some initialization work
}
Human(const Human& rhs); // the default ctor I don't care about
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我有一个const Human对象
const Human one_I_do_not_care; // then play with that
Human the_human_I_care(one_I_do_not_care) // now create another one
Run Code Online (Sandbox Code Playgroud)
最后一行是使用模板ctor还是默认的ctor?我的理解是"const"会取消模板ctor的资格,我是否正确?
Human the_human_I_care(one_I_do_not_care) // line in question
Run Code Online (Sandbox Code Playgroud)
通过const取消模板ctor的资格,我的意思是添加const然后它将不匹配模板ctor,不是它仍然匹配两个,但编译器选择一个.
最后一行是使用模板ctor还是默认的ctor?我的理解是
const将取消模板ctor的资格,我是否正确?
不,你正在通过一个const资格Human作为参数.由于两个构造函数都能很好地匹配(即:如果模板将被实例化为a const Human&),则非模板构造函数优先于模板(并且不会const Human&发生参数的模板实例化).
| 归档时间: |
|
| 查看次数: |
240 次 |
| 最近记录: |