Function object research-sixteen listn Analysis

Source: Internet
Author: User

List2 inherits storage2 to contain the A1 _ and A2 _ members _

struct logical_and;struct logical_or;template< class A1, class A2 > class list2: private storage2< A1, A2 >{private:    typedef storage2< A1, A2 > base_type;public:    list2( A1 a1, A2 a2 ): base_type( a1, a2 ) {}    A1 operator[] (boost::arg<1>) const { return base_type::a1_; }    A2 operator[] (boost::arg<2>) const { return base_type::a2_; }    A1 operator[] (boost::arg<1> (*) ()) const { return base_type::a1_; }    A2 operator[] (boost::arg<2> (*) ()) const { return base_type::a2_; }    template<class T> T & operator[] (_bi::value<T> & v) const { return v.get(); }    template<class T> T const & operator[] (_bi::value<T> const & v) const { return v.get(); }    template<class T> T & operator[] (reference_wrapper<T> const & v) const { return v.get(); }    template<class R, class F, class L> typename result_traits<R, F>::type operator[] (bind_t<R, F, L> & b) const { return b.eval(*this); }    template<class R, class F, class L> typename result_traits<R, F>::type operator[] (bind_t<R, F, L> const & b) const { return b.eval(*this); }    template<class R, class F, class A> R operator()(type<R>, F & f, A & a, long)    {        return unwrapper<F>::unwrap(f, 0)(a[base_type::a1_], a[base_type::a2_]);    }    template<class R, class F, class A> R operator()(type<R>, F const & f, A & a, long) const    {        return unwrapper<F const>::unwrap(f, 0)(a[base_type::a1_], a[base_type::a2_]);    }    template<class F, class A> void operator()(type<void>, F & f, A & a, int)    {        unwrapper<F>::unwrap(f, 0)(a[base_type::a1_], a[base_type::a2_]);    }    template<class F, class A> void operator()(type<void>, F const & f, A & a, int) const    {        unwrapper<F const>::unwrap(f, 0)(a[base_type::a1_], a[base_type::a2_]);    }    template<class A> bool operator()( type<bool>, logical_and & /*f*/, A & a, int )    {        return a[ base_type::a1_ ] && a[ base_type::a2_ ];    }    template<class A> bool operator()( type<bool>, logical_and const & /*f*/, A & a, int ) const    {        return a[ base_type::a1_ ] && a[ base_type::a2_ ];    }    template<class A> bool operator()( type<bool>, logical_or & /*f*/, A & a, int )    {        return a[ base_type::a1_ ] || a[ base_type::a2_ ];    }    template<class A> bool operator()( type<bool>, logical_or const & /*f*/, A & a, int ) const    {        return a[ base_type::a1_ ] || a[ base_type::a2_ ];    }    template<class V> void accept(V & v) const    {        base_type::accept(v);    }    bool operator==(list2 const & rhs) const    {        return ref_compare(base_type::a1_, rhs.a1_, 0) && ref_compare(base_type::a2_, rhs.a2_, 0);    }};

Code 1 defines two empty classes, indicating logic and or.

The following four functions are more than list1:

    A1 operator[] (boost::arg<1>) const { return base_type::a1_; }    A2 operator[] (boost::arg<2>) const { return base_type::a2_; }    A1 operator[] (boost::arg<1> (*) ()) const { return base_type::a1_; }    A2 operator[] (boost::arg<2> (*) ()) const { return base_type::a2_; }

Other list3-list9 is similar.

Conclusion: The listn template inherits the storagen template to obtain the ability to save the corresponding number of placeholders. The array Operator [] () is provided to obtain these placeholder objects. Because operator () is provided, it is also a function object and the operator ()() the function can be represented by the execution parameter F and pass its own parameter to F.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.