Boost logo

Boost Users :

Subject: Re: [Boost-users] compilation problem and a couple ofboost::variant questions
From: Petros (pmamales_at_[hidden])
Date: 2012-07-11 17:24:03


Thank you Larry,
tried it out with the same error messages.
All the Best,
Petros

-----Original Message-----
From: Larry Evans
Sent: Wednesday, July 11, 2012 4:04 PM
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] compilation problem and a couple ofboost::variant
questions

On 07/11/12 13:32, Petros wrote:
> Hi,
> I am trying to write some operator that multiplies two variants, each
[snip]
> First here is the code:
>
> #include <boost/variant.hpp>
> using boost::variant;
>
> struct Zero1{ double operator[]( const size_t i ) const { return 0L
> ; } };
> struct One1{ double operator[]( const size_t i ) const { return 1L
> } };
> struct Zero2{ double operator[]( const size_t i ) const { return 0L
> ; } };
> struct One2{ double operator[]( const size_t i ) const { return 1L
> } };
> struct Vector1{ double operator[]( const size_t i ) const {return
> double( i ) ; } };
> struct Vector2{ double operator[]( const size_t i ) const {return
> double( 2*i ) ; } };
> struct
> subscript_operator:public boost::static_visitor<double>{
> const size_t i_;
> subscript_operator( const size_t i ):i_(i){}
>
> template<typename _v>
> double operator()( _v const & v ) const {
> return v[i] ;
> }
> };
>
>
> template <typename _V, typename _Z, typename _U>
> class VariantT
> :public variant< _V, _Z, _U >
> {
> public:
> typedef typename _Z zero_type ;
> typedef typename _U unit_type ;
>
> typedef typename VariantT<_V, _Z, _U> self_type ;
> typedef typename variant< _V, _Z, _U > variant_type ;
>
> template < typename _V>
> explicit VariantT( _V const & v )
> :variant_type(v)
> {}
> VariantT()
> {}
> VariantT( self_type const & v )
> :variant_type(static_cast<const variant_type&>(v))
> {}
> template < typename _V>
> VariantT & operator = ( _V const & v ) {
> *static_cast<variant_type*>(this) = static_cast<const
> variant_type&>(v);
> return(*this);
> }
> VariantT & operator = ( self_type const & v ) {
> *static_cast<variant_type*>(this) = v;
> return(*this);
> }
>
> double operator[]( const size_t i ) const {
> boost::apply_visitor( subscript_operator(i) , *this ) ;
> }
>
> bool isZero() const { return which() == 1 ; }
> bool isUnit() const { return which() == 2 ; }
>
> private:
> };
>
> template < typename _V1, typename _V2 >
> class MultVarVar{
> public:
> MultVarVar( _V1 const & v1, _V2 const & v2 )
> :v1_(v1),v2_(v2)
> {}
> ~MultVarVar()
> {}
>
> typedef typename _V1::zero_type zero_type ;
> typedef typename _V1::unit_type unit_type ;
>
> double operator[]( const size_t i ) const {
> return
> boost::apply_visitor ( elementwise_visitor( i ), v1, v2 ) ;
> }
>
> private:
> _V1 const & v1_ ;
> _V2 const & v2_ ;
>
> struct
> elementwise_visitor
> : public boost::static_visitor< double > {
> const size_t & i_;
> public:
> elementwise_visitor( const size_t & i )
> :i_(i)
> {}
> template <typename _V1, typename _V2 >
> double operator()( _V1 const & v1, _V2 const & v2) const {
> return v1[i_] * v2[i_] ;
> }
> };
> };
>
> template < typename _V1, typename _V2 >
> class MultVarVarResult
> :public variant<typename MultVarVar<_V1, _V2>, typename
> MultVarVar<_V1,_V2>::zero_type, typename MultVarVar<_V1,_V2>::unit_type,
> _V1, _V2 > {
> public:
> typedef typename variant<
> typename MultVarVar<_V1, _V2 > ,
> typename MultVarVar<_V1,_V2>::zero_type,
> typename MultVarVar<_V1,_V2>::unit_type,
> _V1,
> _V2 > variant_type;
> typedef typename MultVarVarResult<_V1, _V2> self_type ;
>
> typedef typename _V1::zero_type zero_type ;
> typedef typename _V1::unit_type unit_type ;
>
> template<typename _V>
> /*explicit*/ MultVarVarResult( _V const & v )
> :variant_type(v)
> {}
> MultVarVarResult( self_type const & v )
> :variant_type(static_cast<const variant_type&>(v))
> {}
> template < typename _V>
> MultVarVarResult & operator = ( _V const & v ) {
> *static_cast<variant_type*>(this) = static_cast<const
> variant_type&>(v);
> return(*this);
> }
> MultVarVarResult & operator = ( self_type const & v ) {
> *static_cast<variant_type*>(this) = v;
> return(*this);
> }
>
> double operator[]( const size_t i ) const {
> boost::apply_visitor( subscript_operator(i), *this ) ;
> }
> private:
> };
>
> template <typename _V1, typename _V2>
> MultVarVarResult<_V1, _V2> operator * ( _V1 const & v1, _V2 const & v2 ) {
>
> if ( v1.isZero() || v2.isZero() )
> return
> MultVarVarResult<_V1, _V2>( typename
> MultVarVar<_V1,_V2>::zero_type() ) ;
> if ( v1.isUnit() )
> return
> MultVarVarResult<_V1, _V2>( v2 );
> if ( v2.isUnit() )
> return
> v1 ;
> else
> return
> MultVarVarResult<_V1, _V2>( MultVarVar<_V1, _V2>( v1, v2 ) ) ;
> }
>
>
> typedef VariantT< Vector1, Zero1, One1 > Variant1;
> typedef VariantT< Vector2, Zero2, One2 > Variant2;
>
> int main(){
> Variant1 v1;
> v1 = Zero1() ;
> Variant2 v2;
> v2 = One2() ;
>
> auto x = v1 * v2 ;
>
> return x[0] == double(0) ;
> }
>
[snip]

The attached contains a few minor corrections; however, there's still
a few I don't know about. In particular, there's the shadowing.
Maybe you have some idea how to correct some of the remaining
problems.

Compilation with gcc4.8 shows errors shown in 2nd attachment.

-regards
Larry

_______________________________________________
Boost-users mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users


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