Boost logo

Boost :

Subject: Re: [boost] Request for help in porting colony to boost from experienced container boost dev
From: Glen Fernandes (glen.fernandes_at_[hidden])
Date: 2015-08-28 07:20:55


On Fri, Aug 28, 2015, [Matt] wrote:
> I'm pretty sure I'm not - I watched the entirety of his Data Driven Design
> speech at cppcon - several times - and if you watch that he's very specific.

When you write application code, you're free to do what you want (and
avoid caring about exception handling entirely). When you write
library code, you're not. You may have people consuming your library
code who have exception handling needs that are not your own. Look at
the two possible implementations of f() below. Mike isn't telling you
to put the former in public/library code over the latter:

namespace lib {
    template<class T>
    T* f()
    {
        void* p = aligned_alloc(alignof(T), sizeof(T));
        if (!p) {
            return nullptr;
        }
        return ::new(p) T();
    }
}

... versus ...

namespace lib {
    template<class T>
    T* f()
    {
        void* p = aligned_alloc(alignof(T), sizeof(T));
        if (!p) {
            return nullptr;
        }
        try {
            return ::new(p) T();
        } catch (...) {
            aligned_free(p);
            return nullptr;
        }
    }
}

> That's fine, it's just not something I immediately felt was important, but I
> can see that it is for boost-

For Boost. For the C++ standard library. For any good library. As
above, there's a difference between writing application code and
[good] library code.

> If you could help me with some basic code here, I'd appreciate it-
> I understand what you're saying, but am unsure whether you're meaning to
> wrap the allocator instance, or something else entirely.

Look up boost::compressed_pair or the EBO, give it an attempt, and we
can review it. I hope that you understand the issue, though. Consider
what you have already:

template</* ... */>
class colony {
public:
    // ...
private:
    // ...
    element_allocator_type a;
    group_allocator_type b;
    // ...
};

If the allocator provided is a stateless allocator who has no members
(e.g. std::allocator<>), sizeof(a) is still 1, and sizeof(b) is still
1, so sizeof(colony) is increased by 2 bytes when it doesn't have to
be.

Best,
Glen


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk