Using local class with std::accumulate?

I'm trying to sum the size of buffers in a list.

I propose the following solution, using a function local class:

1
2
3
4
5
6
7
8
size_t size_() const
{
    struct accum_buf_size
    {
        inline size_t operator()(size_t sum, const boost::asio::const_buffer &buf) const { return sum + boost::asio::buffer_size(buf); }
    };
    return std::accumulate(outbound.begin(), outbound.end(), size_t(0), accum_buf_size());
}


where outbound is:

std::list<boost::asio::const_buffer> outbound; // messages waiting to be written to socket

Compiling fails on line 7, the call to std::accumulate:
no matching function for call to ‘accumulate(std::_List_const_iterator<boost::asio::const_buffer>, std::_List_const_iterator<boost::asio::const_buffer>, size_t, cmt::serialise::binary::tx_object::size_() const::accum_buf_size)’


If I don't use a local class it works.

ie, this works:

1
2
3
4
5
6
7
8
9
struct accum_buf_size
{
    inline size_t operator()(size_t sum, const boost::asio::const_buffer &buf) const { return sum + boost::asio::buffer_size(buf); }
};

size_t size_() const
{
    return std::accumulate(outbound.begin(), outbound.end(), size_t(0), accum_buf_size());
}


Can anyone shed light on why using a function local class fails?

Last edited on
Never mind - it's explicitly forbidden by the standard.

14.3.1: A local type, a type with no linkage, an unnamed type or a type compounded from any of these types shall not be used as a template-argument for a template type-parameter.

I believe C++0x will rescind this
Topic archived. No new replies allowed.