How to resolve the ambiguity between a conversion constructor and a regular constructor?

I was looking forward to eliminating the ambiguity between the normal constructor and the automatic conversion constructor.

as far as I know, this ambiguity can be partially removed by declaring the normal constructor as explicit, so compilers avoid using this constructor as a conversion constructor, for example:

#include <iostream>
#include <fstream>

class Integer{
  int i ;

public:

  explicit  Integer (const int _i) : i(_i) {} //Normal constructor
  Integer (const int& ii ) : i (ii) {}        // conversion constructor
  operator int() {return int (i) ;}         // Auto-conversion operator

  friend std::ostream & operator <<(std::ostream &os, const Integer io ) {
    std::cout << io.i  ;
    return os ;
}

};


int main() {

// Integer io (5); call of overloaded ‘Integer(int)’ is ambiguous error 
  Integer io2 = 20 ;          // conversion constructor
  std:: cout << io2 << "\n" ;
  int i = io2 ;               // auto-conversion operator
  std:: cout << i << "\n" ;
}

conclusion:

20
20

my questions:

1- Is there a standard way to force a constructor to be used as a transform constructor, since there is a standard way to force use as a regular constructor?

2 - , , , , ?

+4
1

, ?

, explicit.

, , , , contructor ?

, . . , .. . .

+1

All Articles