On Friday, 20 September 2013 at 08:20:48 UTC, Szymon Gatner wrote:
The struct was only an example, obviously you can use proper class implementations with data hiding etc. You will also get at least 1 virtual call because a "concept" is always an abstract class.

Direct use of the value type has no indirections.

That being said, I've been using this technique in other places in my code this far too, for example: my C++ ranges (yeah idea stolen from D) are templates so can be composed for free but there are abstractions like InputRange<> which "erase" (this word is stupid btw, no type is erased really) underlying type and provide convenient abstraction in places. I also use this technique for my std containers. Vector is a std::vector with polymorphic allocator, again it holds Allocator<> by value but underlying implementation is (often composition) of allocator templates. In allocator's case I do explicitly want container alloc to be polymorphic type so that my interfaces never depend on allocator type.

Difference now for me is that I did sometimes turn value types into polymorphic types using this technique (like with allocators) but it never occurred to me that other wan can be beneficial too.

Well Adapter is suppose to *change* interface to adapt to client needs so this is a bit of a strech to call it that here, but yeah, this technique called ("external polymorphism" idiom) can be used to adapt . This really just is a (smart)pointer with a full interface of underlying object (which also adds benefit or using "." instead of "->" on target).

Well, I won't argue about naming, for me when a type is a wrapper which provides desired interface to that type is an adapter :P.

I agree that this idiom is useful and I use it as needed too.

Reply via email to