Advanced C++ May someone to comment what each line is trying to do generally

Advanced C++ May someone do comment what each line is trying to do generaly
Why so many namespace definition, Why so many Template, Are they expensive
and slow, ???

// reference_accumulator.hpp
// Copyright 2005 Eric Niebler. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at


#include <boost/ref.hpp>
#include <boost/mpl/always.hpp>
#include <boost/parameter/keyword.hpp>
#include <boost/accumulators/framework/depends_on.hpp> // for feature_tag
#include <boost/accumulators/framework/accumulator_base.hpp>
#include <boost/accumulators/framework/extractor.hpp>

namespace boost { namespace accumulators

namespace impl
// reference_accumulator_impl
template<typename Referent, typename Tag>
struct reference_accumulator_impl
: accumulator_base
typedef Referent &result_type;

template<typename Args>
reference_accumulator_impl(Args const &args)
: ref(args[parameter::keyword<Tag>::get()])

result_type result(dont_care) const
return this->ref;

reference_wrapper<Referent> ref;
} // namespace impl

namespace tag
// reference_tag
template<typename Tag>
struct reference_tag

// reference
template<typename Referent, typename Tag>
struct reference
: depends_on<>
typedef mpl::always<accumulators::impl::reference_accumulator_impl<Referent, Tag> > impl;

namespace extract
BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, reference, (typename)(typename))
BOOST_ACCUMULATORS_DEFINE_EXTRACTOR(tag, reference_tag, (typename))
This is part of the boost.accumulators library, specifically, this one:

There are so many namespaces and templates because this is (a very small small piece of) a fairly generic library, and no, they are cheap and fast, like most well-written C++ templates.

as for going line-by-line.. it may not make sense if you're not actually using the library from which this is part.. to hit the highlights:

"struct reference" defines what that library calls a "Feature".

Every feature is required to define a typedef "impl", which resolves to the accumulator type that implements that feature -- "accumulators::impl::reference_accumulator_impl" in this case.

That struct reference_accumulator_impl is what actually defines the accumulator class. Every accumulator is required to
1. provide a typedef "result_type", which this one defines as Referent&
2. provide a constructor that takes an argument pack (this one defines one that pulls the Tag'ged argument out and stores a reference to it in the private member of type boost::reference_wrapper)
3. provide a result() public member function, which returns something of type result_type -- this one defines one that returns the contents of the wrapper reference.

and finally, this file defines a tag called reference_tag, which is how you use this feature in your code.
Thanks is very knowledgeable, your explanation, just that I had a feeling before that, only very few people know what's going on, now you inspire me to get my head down and burn my neurons to get to your level, thanks for your time , One more question is you use boost library, are we able to avoid using win32 or 64 at all .....
Topic archived. No new replies allowed.