automatic type conversion

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Rahul

    automatic type conversion

    Hi Everyone,

    I have the following code,


    class B;

    class A
    {
    public : operator B();
    };

    class B
    {
    public : B()
    {
    }
    B(const A& ref)
    {
    printf("destina tion conversion\n");
    }

    B(const B& ref)
    {
    printf("copy constructor\n") ;
    }
    };

    A::operator B()
    {
    printf("source conversion\n");
    return B();
    }
    int main()
    {
    A obj;
    B obj1 = obj;
    return(0);
    }

    so the output is

    destination conversion.

    There are actually two ways to convert from obj to obj1
    1) destination conversion, constructor function of B
    2) source conversion, operator function of A and then copy constructor
    of B

    The compiler has selected the first option, where as i expected a
    ambiguity compile time error, what does the standard say for this?
  • Victor Bazarov

    #2
    Re: automatic type conversion

    Rahul wrote:
    Hi Everyone,
    >
    I have the following code,
    >
    >
    class B;
    >
    class A
    {
    public : operator B();
    };
    >
    class B
    {
    public : B()
    {
    }
    B(const A& ref)
    {
    printf("destina tion conversion\n");
    }
    >
    B(const B& ref)
    {
    printf("copy constructor\n") ;
    }
    };
    >
    A::operator B()
    {
    printf("source conversion\n");
    return B();
    }
    int main()
    {
    A obj;
    B obj1 = obj;
    return(0);
    }
    >
    so the output is
    >
    destination conversion.
    >
    There are actually two ways to convert from obj to obj1
    1) destination conversion, constructor function of B
    2) source conversion, operator function of A and then copy constructor
    of B
    >
    The compiler has selected the first option, where as i expected a
    ambiguity compile time error, what does the standard say for this?
    Your declaration/definition/initialisation statement

    B obj1 = obj;

    is actually another form of writing

    B obj1((B(obj)));

    (and the copying from the temporary of type B to 'obj1' is allowed
    to be optimized away by the compiler). As you can see, the temporary
    is created from 'obj'. There are two ways to create it: by means of
    the converting constructor in B or by means of creating *another*
    temporary from A::operator B() and then copy-constructing the original
    temporary. The compiler chooses the short way, I guess.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Rahul

      #3
      Re: automatic type conversion

      On Dec 11, 11:33 pm, "Victor Bazarov" <v.Abaza...@com Acast.netwrote:
      Rahul wrote:
      Hi Everyone,
      >
      I have the following code,
      >
      class B;
      >
      class A
      {
      public : operator B();
      };
      >
      class B
      {
      public : B()
      {
      }
      B(const A& ref)
      {
      printf("destina tion conversion\n");
      }
      >
      B(const B& ref)
      {
      printf("copy constructor\n") ;
      }
      };
      >
      A::operator B()
      {
      printf("source conversion\n");
      return B();
      }
      int main()
      {
      A obj;
      B obj1 = obj;
      return(0);
      }
      >
      so the output is
      >
      destination conversion.
      >
      There are actually two ways to convert from obj to obj1
      1) destination conversion, constructor function of B
      2) source conversion, operator function of A and then copy constructor
      of B
      >
      The compiler has selected the first option, where as i expected a
      ambiguity compile time error, what does the standard say for this?
      >
      Your declaration/definition/initialisation statement
      >
      B obj1 = obj;
      >
      is actually another form of writing
      >
      B obj1((B(obj)));
      >
      (and the copying from the temporary of type B to 'obj1' is allowed
      to be optimized away by the compiler). As you can see, the temporary
      is created from 'obj'. There are two ways to create it: by means of
      the converting constructor in B or by means of creating *another*
      temporary from A::operator B() and then copy-constructing the original
      temporary. The compiler chooses the short way, I guess.
      >
      V
      --
      Please remove capital 'A's when replying by e-mail
      I do not respond to top-posted replies, please don't ask
      But the moment i have the following code,

      A obj;
      B obj1;
      obj1 = obj;

      I get an error, and the compiler is not able to decide between the
      constructor conversion and the operator conversion... So i expected a
      similar error in the initial posted code... Does anyone know what the
      standard says for these cases?

      Comment

      • red floyd

        #4
        Re: automatic type conversion

        Rahul wrote:
        >
        class B;
        >
        class A
        {
        public : operator B();
        };
        >
        class B
        {
        public : B()
        {
        }
        B(const A& ref)
        {
        printf("destina tion conversion\n");
        }
        [remainder redactged]
        This is homework. vairavans@gmail .com posted the exact same question.

        To vairavans and Rahul, please see FAQ 5.2:



        Comment

        • Rahul

          #5
          Re: automatic type conversion

          On Dec 12, 12:14 am, red floyd <no.s...@here.d udewrote:
          Rahul wrote:
          >
          class B;
          >
          class A
          {
          public : operator B();
          };
          >
          class B
          {
          public : B()
          {
          }
          B(const A& ref)
          {
          printf("destina tion conversion\n");
          }
          [remainder redactged]
          >
          This is homework. vairav...@gmail .com posted the exact same question.
          >
          To vairavans and Rahul, please see FAQ 5.2:https://www.parashift.com/c++-faq-li...t.html#faq-5.2
          I don't understand what possible home work do u make out of it?
          I'm just trying to understand cases where compiler spots an
          ambiguity... besides the complexity just increases with operator=,
          copy constructor and their combination with source converter or
          destination converter or both...
          Naturally one would like to clear out the basic simple cases
          first...

          Comment

          • red floyd

            #6
            Re: automatic type conversion

            Rahul wrote:
            On Dec 12, 12:14 am, red floyd <no.s...@here.d udewrote:
            >Rahul wrote:
            >>
            >>class B;
            >>class A
            >>{
            >>public : operator B();
            >>};
            >>class B
            >>{
            >>public : B()
            >> {
            >> }
            >> B(const A& ref)
            >> {
            >> printf("destina tion conversion\n");
            >> }
            >>[remainder redactged]
            >This is homework. vairav...@gmail .com posted the exact same question.
            >>
            >To vairavans and Rahul, please see FAQ 5.2:https://www.parashift.com/c++-faq-li...t.html#faq-5.2
            >
            I don't understand what possible home work do u make out of it?
            I'm just trying to understand cases where compiler spots an
            ambiguity... besides the complexity just increases with operator=,
            copy constructor and their combination with source converter or
            destination converter or both...
            Naturally one would like to clear out the basic simple cases
            first...

            If it's not homework, then why did you and vairavans post identical code
            (down to formatting!) with the same question?

            Comment

            • James Kanze

              #7
              Re: automatic type conversion

              On Dec 11, 7:28 pm, Rahul <sam_...@yahoo. co.inwrote:
              I have the following code,
              class B;
              class A
              {
              public : operator B();
              };
              class B
              {
              public : B()
              {
              }
              B(const A& ref)
              {
              printf("destina tion conversion\n");
              }
              B(const B& ref)
              {
              printf("copy constructor\n") ;
              }
              };
              A::operator B()
              {
              printf("source conversion\n");
              return B();}
              int main()
              {
              A obj;
              B obj1 = obj;
              return(0);
              }
              so the output is
              destination conversion.
              There are actually two ways to convert from obj to obj1
              1) destination conversion, constructor function of B
              2) source conversion, operator function of A and then copy constructor
              of B
              The compiler has selected the first option, where as I
              expected a ambiguity compile time error, what does the
              standard say for this?
              That you should see "source conversion" (which is also what I
              get from g++ and Sun CC). The requirement in "B obj1 = obj;" is
              that obj must be converted into a B (which is then used as an
              argument for the copy constructor). A::operator B() is an exact
              match, in every detail. B( A const& ) is classified as an exact
              match, but does require adding a const, which in this case, the
              compiler shoul use as a tie-breaker.

              Change the code to:
              A const obj ;
              B obj1 = obj ;
              and you should see "destinatio n conversion", since A::operator
              B() can no longer be called on obj. (You'll also have to
              provide a user defined default constructor for A.)

              Declare A::operator B() const (which it probablyl should be),
              and the conversions will be ambiguous (but g++ allows it, using
              "destinatio n conversions"---an error in g++).

              In practice, of course, it shouldn't matter---you should never
              provide round-trip implicit conversions anyway.

              --
              James Kanze (GABI Software) email:james.kan ze@gmail.com
              Conseils en informatique orientée objet/
              Beratung in objektorientier ter Datenverarbeitu ng
              9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

              Comment

              Working...