Opened 13 years ago

Closed 13 years ago

#3731 closed Feature Requests (wontfix)

array without padding

Reported by: anonymous Owned by: No-Maintainer
Milestone: Boost 1.42.0 Component: array
Version: Boost 1.41.0 Severity: Problem
Keywords: Cc:

Description

Sometimes you have a struct with array in it:

#pragma pack(push, 1)
struct X
{
 int i[10];
};
#pragma pack(pop)

And you want to use boost::array :

#pragma pack(push, 1)
struct X
{
 boost::array<int, 10> i;
};
#pragma pack(pop)

The problem that boost::array is not surrounded with #pragma pack.

It would be nice to add a new class boost::array_no_padding with #pragma pack surrounded and with static assert for size checking.

#if !BOOST_SUPPORTS_PRAGMA_PACK &&
    !BOOST_SUPPORTS_ALIGNMENT_PREFIX &&
    !BOOST_SUPPORTS_ALIGNMENT_SUFFIX
#error Cannot specify alighnemnt
#endif

namespace boost {


#if BOOST_SUPPORTS_PRAGMA_PACK
#define ALIGNMENT_SET
#pramga pack(push, 1)
#endif

template<class T, std::size_t N>
class
   #if !defined(ALIGNMENT) && BOOST_SUPPORTS_ALIGNMENT_PREFIX
   // Could be __declspec(align(1))
   BOOST_ALIGNMENT_PREFIX
   #define ALIGNMENT_SET
   #endif
   array_no_padding

   #if !defined(ALIGNMENT) && BOOST_SUPPORTS_ALIGNMENT_SUFFIX
   // Could be __attribute__((aligned(1)))
   BOOST_ALIGNMENT_SUFFIX
   #define ALIGNMENT_SET
   #endif
   
   // Can derive from boost::array or copy-paste all the content
    :  boost::array
   {
      // ...


     // Check class size with the array size
     BOOST_STATIC_ASSERT( sizeof(*this) == sizeof(T[N]) );
   }


#if BOOST_SUPPORTS_PRAGMA_PACK
#pragma pack(pop)
#endif

Thanx !

Change History (2)

comment:1 by anonymous, 13 years ago

Component: Nonearray
Owner: set to No-Maintainer

comment:2 by Steven Watanabe, 13 years ago

Resolution: wontfix
Status: newclosed

I think it would be a bad idea to clutter the interface with such a specialized use case. If you need this, use a plain array instead of Boost.Array.

Note: See TracTickets for help on using tickets.