lundi 6 mars 2017

C++ Templates std::array

I'm thinking about creating an in memory Unicode Character database (ucd) in C++. To store the codepoint for e.g. UTF-16 the container must be:

  • small in memory size: No std::vector, std::string etc...
  • have both randomaccess to its codeunits and the data aligned as a whole.
  • must be fast comparable
  • must have compile time flexible size for codepoints with surrogates

To container that comes close is std::array. But I want to hide the N by filling it using a constructor with parameter pack:

#include <array>
#include <utility>

template <typename CUT>
struct ucodepoint_storage
{
  typedef typename CUT            value_type;
  typedef unsigned char           size_type;

  template <size_type N>
  struct sizer
  {
    static constexpr size_type units = N;
    static constexpr size_type bytes = (N * sizeof(value_type));
  };

  using sizer_type = sizer<1>;

  template<typename... Chars>
  constexpr ucodepoint_storage(Chars&&... chars)
    : m_arrData(std::move(std::array<value_type, sizeof...(chars)>{ chars... }))
  {
    using sizer_type = sizer<sizeof...(chars)>;
  }

private:
  const std::array<value_type, sizer_type::units>     m_arrData;
};

Above does not compile when amount of args does not match the default 1.

Is this possible? Everything is compile time known and it looks like different types of same member just is not possible. I see it as a pImpl idom without the pointers because they will also take up to much memory on my 64 bits machine.

Aucun commentaire:

Enregistrer un commentaire