Is it permissible to have two implicit translations when building an object in C ++?

Given the following two constructor signatures, is it possible to build a Couplec Couple("George", "Nora")? My compiler complains about the error shown below. If I call him Couple(std::string("George"), std::string("Nora")), he will compile OK. I assume that the problem with implicit casting is surprising to me, because despite the fact that char * for the string will be fine.

class Person
{
    public:
        Person(const std::string& name);
};

class Couple
{
    public:
        Coordinate(const Person& p1, const Person& p2, const Optional<Person>& = Optional<Person>());
};

TestCouple.cpp:69: error: no matching function for call to `Couple::Couple(const char[7], const char[5])'
TestCouple.h:24: note: candidates are: Couple::Couple(const Person&, const Person&, const Optional<fox::Person>&)
+5
source share
3 answers

Indeed, a transform sequence cannot contain more than one implicit user transform; the standard indicates this in C ++ 11 12.3 / 4:

At most 1 user-defined transform (constructor or transform function) is implicitly applied to a single value.

( char const[] std::string Person), .

+12

, . , , , Couple(std::string("a"), std::string("b")) Couple(Person("a"), Person("b")), Couple("a", "b") , . , , .

+6

. A B, B C, , A C.

//given three objects as
A a;
B b'
C c;

//premises 
b = a; //a can convert into b (implicitly)
c = b; //b can convert into c (implicitly)

//then it does not follow this
c = a; //a CANNOT convert into c (implicitly)

//you need to write this at least
c = static_cast<B>(a); //ok
+1

All Articles