c++ - Why the following code is error (about overload resolution) -


this question has answer here:

given following code in gcc-4.8.1

struct base { };  struct derive : private base { };  void fn(base, int);  struct conv {     operator base() const;     operator derive(); };  int main() {     conv c;     fn(c, 0);     return 0; } 

when gave above code, got error.i think compiler select conv::operator base() compiler selected conv::operator derive()

but when gave following code, compiler selected conv::operator base()

struct base { };  struct derive// : private base { };  void fn(base, int);  struct conv {     operator base() const;     operator derive(); };  int main() {     conv c;     fn(c, 0);     return 0; } 

the key access specifiers not checked until conversion sequence selected, code feels like:

struct base {}; struct derived : base {}; struct conv {     operator base() const;    operator derived(); }; void fn(base); int main() {    conv c;    fn(c); } 

at point there different valid conversion sequences:

  • adding const qualification, user conversion base
  • user conversion derived, derived base conversion

the second conversion better conversion sequence , selected.

this treated in standard in 13.3.3.1/2:

implicit conversion sequences concerned type, cv-qualification, , value category of argument , how these converted match corresponding properties of parameter. other properties, such lifetime, storage class, alignment, or accessibility of argument , whether or not argument bit-field ignored. so, although implicit conversion sequence can defined given argument-parameter pair, conversion argument parameter might still ill-formed in final analysis.


Comments

Popular posts from this blog

design - Custom Styling Qt Quick Controls -

Unable to remove the www from url on https using .htaccess -