Topic: implicit conversion involving class templates


Author: nesotto@cs.auc.dk ("Thorsten Ottosen")
Date: Thu, 18 Dec 2003 18:31:27 +0000 (UTC)
Raw View
Hi Gurus,

should the follwing program compile? vc7.1 say yes, como 4.3.3 and g++ 3.3.1
say no:

Thanks

Thorsten

-----------------------------

template< typename T >
class Wrapper
{
    T t_;
public:
    Wrapper( T t ) : t_( t ) // implicit custruction important
    { }

    T value() const
    {
        return t_;
    }
};


class Int
{
    int i_;

public:
    Int() : i_( 0 )
    { }
    Int( const Int& i ) : i_( i.i_ )
    { }
    Int& operator=( const Int& i )
    { i_ = i.i_; return *this; }

    template< typename T >
    Int( const Wrapper<T>& w ); // no definition

    Int( const Wrapper<int>& w )
    {
        i_ = w.value();
    }

    operator int() const
    {
        return i_;
    }
};

#include <iostream>

int main()
{
    Int i = 5;
    std::cout << i;
}


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: francis@robinton.demon.co.uk (Francis Glassborow)
Date: Thu, 18 Dec 2003 19:07:05 +0000 (UTC)
Raw View
In article <3fe1ae61$0$18388$afc38c87@news.optusnet.com.au>, Thorsten
Ottosen <nesotto@cs.auc.dk> writes
>should the follwing program compile? vc7.1 say yes, como 4.3.3 and g++ 3.3.1
>say no:

And they are correct. If VC++7.1 accepts it in conforming mode without
issuing a diagnostic it is wrong.
>
>Thanks
>
>Thorsten
>
>-----------------------------
>
>template< typename T >
>class Wrapper
>{
>    T t_;
>public:
>    Wrapper( T t ) : t_( t ) // implicit custruction important
>    { }
>
>    T value() const
>    {
>        return t_;
>    }
>};
>
>
>class Int
>{
>    int i_;
>
>public:
>    Int() : i_( 0 )
>    { }
>    Int( const Int& i ) : i_( i.i_ )
>    { }
>    Int& operator=( const Int& i )
>    { i_ = i.i_; return *this; }
>
>    template< typename T >
>    Int( const Wrapper<T>& w ); // no definition
>
>    Int( const Wrapper<int>& w )
>    {
>        i_ = w.value();
>    }
>
>    operator int() const
>    {
>        return i_;
>    }
>};
>
>#include <iostream>
>
>int main()
>{
>    Int i = 5;

This fails because it requires two user defined conversions:
int -> Wrapper<int> ->Int and at most one user defined conversion is
allowed in an implicit conversion sequence.

This is yet another case where function style initialisation works and
the assignment form fails:

Int i(5);

is fine.

>    std::cout << i;
>}

--
Francis Glassborow      ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
                             or http://www.robinton.demon.co.uk

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]