Boost logo

Boost Users :

From: Ovanes Markarian (om_boost_at_[hidden])
Date: 2006-06-25 17:17:38


Hello all!
 
I posted the following message
http://lists.boost.org/boost-users/2006/06/20271.php a few days ago, but did
not get any replies.
 
I know there is an error in the const_ref class. But nevertheless the
problem which occures is that my class accepts a vector of supported types
as template parameter.
 
It has two constructors (no default ctor):
 
template<class Seq_>
class my_class
{
    public:
        typedef my_class<Seq_> this_type;
 
        explicit inline my_class(const this_type& copy_from) :... {...}
    
        template<class TypeFromVector>
        explicit inline my_class(typename const_ref<TypeFromVector>::type
inst) :... {...}
};
 
 
Now if I try to initialize my_class with type declared in vector I get an
error:
 
error C2664: 'discriminated_union<Seq_>::discriminated_union(const
discriminated_union<Seq_> &)' : cannot convert parameter 1 from 't1' to
'const discriminated_union<Seq_> &'
        with
        [
            Seq_=types_vector
        ]
        Reason: cannot convert from 't1' to 'const
discriminated_union<Seq_>'
        with
        [
            Seq_=types_vector
        ]
        No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called

No user-defined-conversion operator available that can perform this
conversion, or the operator cannot be called

Ok, if I define the ctor param without typename const_ref<...>::type
everything goes fine, if I use const_ref it fails. const_ref is defined as
follows:

template<class Ty>
struct const_ref
 : boost::add_reference
  <
   typename boost::add_const
   <
    typename boost::remove_reference<Ty>::type
>::type
>
{};

I can understand that the compiler is unable to make a decision which ctor
to use, but I do not get an ambiguaty error. It seems like the compiler
simply quits expanding templates and uses the last most successful ctor
matched before the error. This happens only when I use mpl::vector templated
type in conjunction with templated ctor. Other types like below work fine:

 

template<int D_, class Ty_>
struct my_pair :
 mpl::pair<mpl::int_<D_>, Ty_>
{
 typedef my_pair<D_, Ty_> my_type;

 inline my_pair()
 {}

 explicit inline my_pair( typename const_ref<Ty_>::type ty)
  : second_(ty)
 {}

 explicit inline my_pair(const my_type& other)
  : second_(other.second_)
 {}

 inline my_type& operator =(const my_type& other)
 {
  second_=other.second_;
  return *this;
 }

 second second_;
};

May be some of you have any suggestions.

 

 

Thanks a lot,

Ovanes Markarian



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net