type promotion for some constructors but not others

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

    #1

    type promotion for some constructors but not others

    I have some c++ source where construction via

    Type var(init); // compiles and...

    Type var = init; // does not

    I thought the two are supposed to be completely equivalent? Why does
    type promotion seem to take place on the first line, but not the
    second? Is there a way it can be enabled for the second?

    In this case, init is an enum constant contained within Type. The
    compiler is g++ 4.x. Both lines work with VS8, unless you turn
    language extensions off, in which case you get the g++ behavior.
    (error: conversion from ‘TestChc::Choic es’ to non-scalar type ‘Test’
    requested). Here's the complete code:

    #include <iostream>
    #include <string>
    using namespace std;

    #define TWOCHARINT(A, B) (A+(B<<8)) // FIXME: wrong for big endian
    archs

    template<typena me EnumStrucTstruc t EnumChoices: EnumStrucT
    {EnumChoices(): EnumStrucT() {} // only works if EnumStrucT
    default constructs
    EnumChoices(con st EnumStrucT& arg): EnumStrucT(arg) {}
    EnumChoices& operator=(const EnumStrucT& arg)
    {this->value = arg.value; return *this;} // ^ EnumStrucT handles
    derivations

    bool operator== (const EnumStrucT& arg) const
    {return this->value.asScalar ==arg.value.asS calar;}

    bool operator!= (const EnumStrucT& arg) const
    {return this->value.asScalar !=arg.value.asS calar;}

    const string asString() const
    {return string(EnumStru cT::value.asCha rs, sizeof
    EnumStrucT::val ue.asChars);}

    const uint16_t asScalar() const {return EnumStrucT::val ue;}};

    struct TestChc
    {enum Choices {CN = 0, C1 = '1', CB = TWOCHARINT('A', 'B'), CS = '
    ', CZ = 'Z'};
    union Value
    {uint16_t asScalar;
    char asChars[2];
    Value(const Choices& arg): asScalar(arg) {}} value;
    TestChc(const Choices& arg):value(arg) {}};

    typedef EnumChoices<Tes tChcTest;

    Test& func() {return *(new Test(TestChc::C Z));}

    int main (int, char**)
    {Test t(TestChc::CB); // , tf('X'); is invalid
    // Test t2 = TestChc::CB; // <- this works in VS
    Test t2 = func();
    // Test t2; // this only works if TestChc default constructs
    // Test t2(TestChc::CS) ;
    cout << "size is " << sizeof t2 << endl;
    cout << t.asString() << '\t' << t2.asString() << endl;
    t = t2;
    cout << (t==t2) << endl;
    t = TestChc::C1;
    cout << (t!=t2) << endl;
    return 0;}

    There's a little more than needed there, but the crux of the question
    is why does the first declaration in main compile while the second (if
    uncommented) does not?

    Thanks in advance.
    Zachary
  • acehreli@gmail.com

    #2
    Re: type promotion for some constructors but not others

    On Sep 17, 7:49 am, Zak <Unceld...@gmai l.comwrote:
    I have some c++ source where construction via
    >
        Type var(init);  // compiles and...
    >
        Type var = init;  // does not
    Because the compiler considers only one user defined conversion.
        template<typena me EnumStrucTstruc t EnumChoices: EnumStrucT
        {EnumChoices(): EnumStrucT() {} // only works if EnumStrucT
    default constructs
         EnumChoices(con st EnumStrucT& arg): EnumStrucT(arg) {}
         EnumChoices& operator=(const EnumStrucT& arg)
         {this->value = arg.value; return *this;} // ^ EnumStrucT handles
    derivations
        struct TestChc
        {enum Choices {CN = 0, C1 = '1', CB = TWOCHARINT('A', 'B'),CS = '
    ', CZ = 'Z'};
         union Value
         {uint16_t asScalar;
          char asChars[2];
          Value(const Choices& arg): asScalar(arg) {}} value;
         TestChc(const Choices& arg):value(arg) {}};
        // Test t2 = TestChc::CB; // <- this works in VS
    Two conversions needed, which the compiler will not consider:

    TestChc::Choice s -TestChc
    TestChc -Test

    Ali

    P.S. Your code needs lots of vertical white space to be readable.

    Comment

    • blargg

      #3
      Re: type promotion for some constructors but not others

      In article <gare52$75t$1@a ioe.org>, Andrey Tarasevich
      <andreytarasevi ch@hotmail.comw rote:
      Zak wrote:
      I have some c++ source where construction via

      Type var(init); // compiles and...

      Type var = init; // does not

      I thought the two are supposed to be completely equivalent?
      >
      They are not. They are equivalent only in one particular case when the
      source and destination types are the same.
      Even in that case, the second form can create a temporary if init is not
      Type or something derived from it. That is

      Type var = init;

      can generate either of the following code-wise

      Type var( (Type(init)) );
      Type var(init);

      but must still check for validity as if the first form were used.

      See section 8.5 paragraph 14 of the 2003 C++ standard.

      Comment

      • James Kanze

        #4
        Re: type promotion for some constructors but not others

        On Sep 17, 9:04 pm, blargg....@gish puppy.com (blargg) wrote:
        In article <gare52$75...@a ioe.org>, Andrey Tarasevich
        <andreytarasev. ..@hotmail.comw rote:
        Zak wrote:
        I have some c++ source where construction via
        Type var(init); // compiles and...
        Type var = init; // does not
        I thought the two are supposed to be completely equivalent?
        They are not. They are equivalent only in one particular
        case when the source and destination types are the same.
        Even in that case, the second form can create a temporary if
        init is not Type or something derived from it.
        In theory, at least.
        That is
        Type var = init;
        can generate either of the following code-wise
        Type var( (Type(init)) );
        Type var(init);
        but must still check for validity as if the first form were used.
        Except that if init has type Type, then if the second is legal,
        so is the first. In both cases, you're constructing a Type from
        another type.

        --
        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...