Re: [Boost-users] type_with_alignment
Date: Tue, 13 Jun 2006 19:28:07 +0100 From: "John Maddock" <john@johnmaddock.co.uk>
What's wrong with:
boost::alignment_of<unknown_struct>::value ?
You can feed the result into type_with_alignment<> if that's what you want as well.
John.
if unknown_struct is an incomplete type (which is what I meant, and should have said), alignment_of doesn't compile. Tony
Gottlob Frege wrote:
if unknown_struct is an incomplete type (which is what I meant, and should have said), alignment_of doesn't compile.
If the type is incomplete then you can't use aligned_storage anyway, since you don't know how much storage you'll need! Sorry but you've lost me now. John.
"John Maddock" <john@johnmaddock.co.uk> writes:
Gottlob Frege wrote:
if unknown_struct is an incomplete type (which is what I meant, and should have said), alignment_of doesn't compile.
If the type is incomplete then you can't use aligned_storage anyway, since you don't know how much storage you'll need! Sorry but you've lost me now.
I think what the OP is looking for is called boost::shared_ptr ;-) -- Dave Abrahams Boost Consulting www.boost-consulting.com
On Jun 14, 2006, at 4:56 AM, John Maddock wrote:
Gottlob Frege wrote:
if unknown_struct is an incomplete type (which is what I meant, and should have said), alignment_of doesn't compile.
If the type is incomplete then you can't use aligned_storage anyway, since you don't know how much storage you'll need! Sorry but you've lost me now.
There are applications such as boost::function where aligned storage for an unknown type is useful. In boost::function's case, aligned storage could be used to implement the "short string optimization". The general pattern is: class Container { private: const unsigned buf_size = ...; aligned_storage<buf_size>::type buffer; public: template <class T> Container(const T& t) {/* might stick t into buffer instead of on heap, based on sizeof(T) */} }; On Jun 14, 2006, at 6:55 AM, David Abrahams wrote:
I think what the OP is looking for is called boost::shared_ptr ;-)
shared_ptr can't compete with this application performance-wise. -Howard
Howard Hinnant <hinnant@twcny.rr.com> writes:
There are applications such as boost::function where aligned storage for an unknown type is useful. In boost::function's case, aligned storage could be used to implement the "short string optimization".
And so it is, AFAIK.
The general pattern is:
class Container { private: const unsigned buf_size = ...; aligned_storage<buf_size>::type buffer; public: template <class T> Container(const T& t) {/* might stick t into buffer instead of on heap, based on sizeof(T) */} };
On Jun 14, 2006, at 6:55 AM, David Abrahams wrote:
I think what the OP is looking for is called boost::shared_ptr ;-)
shared_ptr can't compete with this application performance-wise.
Well, it depends how expensive it is to copy your small object. If it turns out that T is a vector, and it fits in your local buffer, when Container is copied it might be a lot cheaper to use shared_ptr. -- Dave Abrahams Boost Consulting www.boost-consulting.com
On Jun 14, 2006, at 10:22 AM, David Abrahams wrote:
Howard Hinnant <hinnant@twcny.rr.com> writes:
There are applications such as boost::function where aligned storage for an unknown type is useful. In boost::function's case, aligned storage could be used to implement the "short string optimization".
And so it is, AFAIK.
The general pattern is:
class Container { private: const unsigned buf_size = ...; aligned_storage<buf_size>::type buffer; public: template <class T> Container(const T& t) {/* might stick t into buffer instead of on heap, based on sizeof(T) */} };
On Jun 14, 2006, at 6:55 AM, David Abrahams wrote:
I think what the OP is looking for is called boost::shared_ptr ;-)
shared_ptr can't compete with this application performance-wise.
Well, it depends how expensive it is to copy your small object. If it turns out that T is a vector, and it fits in your local buffer, when Container is copied it might be a lot cheaper to use shared_ptr.
Sure. I'm responding to the assertion that you need a known complete type to effectively use aligned_storage. My point is that there exist valuable use cases where this assertion is false. The existence of these cases is what makes the defaulted alignment template parameter on aligned_storage valuable. Tony's observation is that this default value is even more valuable if it is dependent upon the buffer size, which again I fully agree with. If you want a 5 byte buffer that is aligned to the "most stringent alignment requirements" (definition found in 5.3.4p10), an alignment of 16 (for example) is likely to be correct but overkill. I had personally settled on 8 for this particular example but Tony makes a convincing argument for 4 being the optimum (all numbers assuming current popular desktop processors and quoted just for example). -Howard
participants (4)
-
David Abrahams
-
Gottlob Frege
-
Howard Hinnant
-
John Maddock