WhatABeautifulWorld
WhatABeautifulWorld

Reputation: 3388

Does 'const' disqualify universal reference?

I have a human class with ctor using universal reference

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
}

Now if I have a const Human object

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

Does the last line use the template ctor or the default ctor? My understanding is the "const" will disqualify the template ctor, am I correct?

Human the_human_I_care(one_I_do_not_care)  // line in question

By const disqualify the template ctor, I mean adding const then it would not match the template ctor, not it still matches two but compiler picks one.

Upvotes: 7

Views: 490

Answers (1)

jfMR
jfMR

Reputation: 24738

Does the last line use the template ctor or the default ctor? My understanding is the const will disqualify the template ctor, am I correct?

No. You are passing a const-qualified Human as an argument. Since both constructors would match equally well (i.e.: if the template would be instantiated to take a const Human&), then the non-template constructor is preferred over the template (and no template instantiation for a const Human& parameter ever occur).

Upvotes: 8

Related Questions