'const'是否取消了普遍参考资格?

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,不是它仍然匹配两个,但编译器选择一个.

眠りネ*_*ネロク 8

最后一行是使用模板ctor还是默认的ctor?我的理解是const将取消模板ctor的资格,我是否正确?

不,你正在通过一个const资格Human作为参数.由于两个构造函数都能很好地匹配(即:如果模板将被实例化为a const Human&),则非模板构造函数优先于模板(并且不会const Human&发生参数的模板实例化).

  • @WhatABeautifulWorld,不,删除的构造函数仍然参与重载解析. (3认同)
  • @WhatABeautifulWorld删除东西不缺.它们遵循通常的重载决策规则.如果选中它们,则会发生错误.这与他们不存在的情况不同*. (3认同)