mercredi 27 juillet 2016

How to detect if a container is guaranteed to have sequence storage

Checking if a sequence container is contiguous in memory.
C++ templates that accept only certain types

I am writing a simple send() method, which internally works with C-style pointers. I would like it to be able to work with all the guaranteed sequence containers. My motivation being twofold:

  • a flexible interface
  • efficiency - using std::array avoids heap allocations.

Here is how far I am:

template <typename Container>
void poll( Container &out )
{
    static_assert( std::is_base_of< std::array<Container::value_type>, Container >::value  ||
                   std::is_base_of< std::vector<Container::value_type>, Container >::value ||
                   std::is_base_of< std::string<Container::value_type>, Container >::value,
                   "A contiguous memory container is required.");
}

Trouble is, std::array requires a second parameter, and that cannot be known at compile time. Is this problem solvable? Possibly by a different approach?

Aucun commentaire:

Enregistrer un commentaire