Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++/C++11 Efficient way to have static array/vector of objects initialized with initializer list, and supporting range-based for

Suppose you want to have a static array of pre-defined values/objects (const or non-const) associated with a class. Possible options are to use std:vector, std::array or C-style array (ie. []), or . For example,

In .hpp:

class MyClass {
public:
    static const std::vector<MyClass> vec_pre; // No efficient way to construct with initializer list, since it always uses Copy Contructor, even when using std::move
    static const std::array<MyClass, 2> arr_pre; // Have to specify size which is inconvenient
    static const MyClass carr_pre[]; // Not compatible with C++11 for-range since size is undefined
};

In .cpp

const std::vector<MyClass> MyClass::vec_pre = { std::move(MyClass{1,2,3}), std::move(MyClass{4,5,6})  }; // NOTE: This still uses copy constructor
const std::array<MyClass, 2> MyClass::arr_pre= { MyClass{1,2,3}, MyClass{4,5,6} };
const ZwSColour ZwSColour::carr_pre[] = {  MyClass{1,2,3}, MyClass{1,2,3} }

When writing this intially, I chose the std::vector since I don't have to specify the size, I get all the goodness of the vector class, and it seems like the modern C++ way to do it. PROBLEM: while testing, I noticed that it would call the Move contructor, but then still call the Copy constructor for each element. The reason for this is the std::initializer_list only allows const access to its members, and so the vector has to copy them from the initializer_list to its own storage. Even though it's only done once at startup, this is inefficient and there doesn't appear to be a way around it, so I looked at other options (std::array and C-array[]).

Second choice was to use std::array, which is also a modern C++ way, and it doesn't suffer from the problem of calling the Copy Constructor for each value since it doesn't need to create copies (Not sure why though exactly ?). std::array also has the benefit that you don't need to wrap each value in std::move(). However, it has the annoyance that you have to specify the size first, so every time you add/remove elements, you have to change the size as well. There are ways around this but none of them are ideal. As @Ricky65 states, you should just be able to do

std::array <int> arr = { 1, 3, 3, 7, 0, 4, 2, 0, 3, 1, 4, 1, 5, 9 }; //automatically deduces its size from the initializer list :)

This leaves me with the last option - the good old C-style array [] - which has the benefits that I don't have to specify the size, and is efficient in that it doesn't call the Copy constructor for each object. Downsides are that it's not really modern C++, and the biggest downside is that if you don't specify the size of the array in the .hpp header, then the C++11 for-range doesn't work as the compiler complains

Cannot use incomplete type 'const MyClass []' as a range

You can overcome this error by either specifying the size of the array in the header (but this is inconvenient and produces hard-to-maintain code, as you need to adjust the size every time you add/remove items from the initializer list), or alternatively use constexpr and completely declare the array and values in the .hpp header

constexpr static MyArray my_array[] = { MyClass{1,2,3}, MyClass{4,5,6} };

NOTE: The constexpr "work-around" will only work for POD's and so cannot be used in this case for Class objects. The above example will result in a compile-time error Invalid use of incomplete type 'MyClass'

I'm trying to write best-practice modern C++ where possible (eg. using copy-and-swap idiom), and so wonder what is the best way to define static arrays for a class...

  • without having to specify the size
  • which don't need to be Copy constructed (or Move constructed either, if possible)
  • which can be used with C++ for-range
  • which don't need to be specified in the header file
  • Should compile/work for Clang/LLVM 3.5, Visual Studio 2013 Update 4 RC, and GCC 4.8.1.

EDIT1: Another post about vector problem of not being able to move values from initializer list

EDIT2: More info on using std::array without the need to specify size, which also creates/uses make_array(), and mentions that there is a proposal for make_array() to become a a standard. Original SO link courtesy of comment by @Neil Kirk.

EDIT3: Another problem with the vector method (at least in this case) is that you cannot iterate over the items using a const T or T. It only allows iteration using const T& (when it's static const) and const T&/T& (when it's static). What's the reason for this limitation ?

Descriptive Answer to solutions

@Yakk's solution appears to be the only solution, and also works on Visual C++ 2013 Update 4 RC.

I find it staggering that such a trivial issue is so difficult to implement using the latest C++11/14 standard.

like image 639
DarkMatter Avatar asked Oct 20 '14 01:10

DarkMatter


2 Answers

The data does not have to be stored within the class. In fact, storing the data within a static member of the class is leaking implementation details.

All you need expose is that the data is available, and that data is global to the class type. This does not involve exposing storage details: all you need to expose is storage access details.

In particular, you want to expose the ability to for(:) loop over the data, and operate on it in a C++11 style way. So expose exactly that.

Store the data in an anonymous namespace in the class's .cpp file in a C-style array (or std::array, I don't care).

Expose in the class the following:

namespace details {
  template<
    class R,
    class iterator_traits,
    class iterator_category,
    bool is_random_access=std::is_base_of<
        std::random_access_iterator_tag,
        iterator_category
    >::value
  >
  struct random_access_support {};
  template<class R, class iterator_traits, class iterator_category>
  struct random_access_support<R, iterator_traits, iterator_category, true> {
    R const* self() const { return static_cast<R const*>(this); }
    template<class S>
    typename iterator_traits::reference operator[](S&&s) const {
      return self()->begin()[std::forward<S>(s)];
    }
    std::size_t size() const { return self()->end()-self()->begin(); }
  };
}

template<class It>
struct range:details::random_access_support<
  range<It>,
  std::iterator_traits<It>,
  typename std::iterator_traits<It>::iterator_category
> {
  using value_type = typename std::iterator_traits<It>::value_type;
  using reference = typename std::iterator_traits<It>::reference;
  using iterator = It;
  using iterator_category = typename std::iterator_traits<It>::iterator_category;
  using pointer = typename std::iterator_traits<It>::pointer;

  It begin() const { return b; }
  It end() const { return e; }

  bool empty() const { return b==e; }
  reference front() const { return *b; }
  reference back() const { return *std::prev(e); }

  range( It s, It f ):b(s),e(f) {}

  range()=default;
  range(range const&)=default;
  range& operator=(range const&)=default;
private:
  It b; It e;
};

namespace details {
  template<class T>
  struct array_view_helper:range<T*> {
    using non_const_T = typename std::remove_const<T>::type;
    T* data() const { return this->begin(); }

    array_view_helper( array_view_helper const& ) = default;
    array_view_helper():range<T*>(nullptr, nullptr){}
    array_view_helper& operator=(array_view_helper const&)=default;

    template<class A>
    explicit operator std::vector<non_const_T, A>() const {
      return { this->begin(), this->end() };
    }
    std::vector<non_const_T> as_vector() const {
      return std::vector<non_const_T>(*this);
    }

    template<std::size_t N>
    array_view_helper( T(&arr)[N] ):range<T*>(arr+0, arr+N) {}
    template<std::size_t N>
    array_view_helper( std::array<T,N>&arr ):range<T*>(arr.data(), arr.data()+N) {}
    template<class A>
    array_view_helper( std::vector<T,A>&vec ):range<T*>(vec.data(), vec.data()+vec.size()) {}
    array_view_helper( T*s, T*f ):range<T*>(s,f) {}
  };
}
// non-const
template<class T>
struct array_view:details::array_view_helper<T> {
  using base = details::array_view_helper<T>;

  // using base::base in C++11 compliant compilers:
  template<std::size_t N>
  array_view( T(&arr)[N] ):base(arr) {}
  template<std::size_t N>
  array_view( std::array<T,N>&arr ):base(arr) {}
  template<class A>
  array_view( std::vector<T,A>&vec ):base(vec) {}
  array_view( T*s, T*f ):base(s,f) {}

  // special methods:
  array_view( array_view const& ) = default;
  array_view() = default;
  array_view& operator=(array_view const&)=default;
};
template<class T>
struct array_view<T const>:details::array_view_helper<const T> {
  using base = details::array_view_helper<const T>;

  // using base::base in C++11 compliant compilers:
  template<std::size_t N>
  array_view( std::array<T const,N>&arr ):base(arr) {}
  array_view( T const*s, T const*f ):base(s,f) {}
  template<std::size_t N>
  array_view( T const(&arr)[N] ):base(arr) {}

  // special methods:
  array_view( array_view const& ) = default;
  array_view() = default;
  array_view& operator=(array_view const&)=default;

  // const T only constructors:
  template<std::size_t N>
  array_view( std::array<T,N> const&arr ):base(arr.data(), arr.data()+N) {}
  template<std::size_t N>
  array_view( std::array<T const,N> const&arr ):base(arr.data(), arr.data()+N) {}
  template<class A>
  array_view( std::vector<T,A> const&vec ):base(vec.data(), vec.data()+vec.size()) {}
  array_view( std::initializer_list<T> il):base(il.begin(), il.end()) {}
};

which is at least a sketch of some view classes. live example

Then expose an array_view<MyClass> as a static member of your class, which is initialized to the array you created in the .cpp file.

range<It> is a range of iterators that acts like a non-owning container. Some tomfoolery is done to block non-constant-time calls to size or [] at the SFINAE level. back() is exposed and simply fails to compile if you call it on invalid iterators.

A make_range(Container) makes range<It> more useful.

array_view<T> is a range<T*> that has a bunch of constructors from contiguous buffer containers, like C-arrays, std::arrays and std::vectors. (actually an exhaustive list).

This is useful because access through an array_view is about as efficient as access to a raw pointer-to-first-element of an array, but we get many of the nice methods that containers have, and it works with range-for loops. In general, if a function takes a std::vector<T> const& v, you can replace it with a function that takes a array_view<T> v and it will be a drop-in replacement. The big exception is operator vector, which is explicit, to avoid accidental allocations.

like image 127
Yakk - Adam Nevraumont Avatar answered Oct 09 '22 14:10

Yakk - Adam Nevraumont


I personally like your constexpr static int my_array[] = {MyClass{1, 2, 3}, MyClass{1, 2, 3}}; I don't think you should shy away from that if a C-style array meets your needs.

If you really want to use std::vector though you could use static const std::vector<MyClass*> vec_pre;. So your .cpp file would have this at the top:

namespace{
    MyClass A{1, 2, 3}, B{1, 2, 3}, C{1, 2, 3};
}
const std::vector<MyClass*> MyClass::vec_pre{&A, &B, &C};

EDIT after DarkMatter's comments:

After reading your comments, it looks like there could be some maintainability hazard to my method. It could still be accomplished like this in your .cpp:

namespace{
    MyClass temp[]{MyClass{1, 2, 3}, MyClass{1, 2, 3}, MyClass{1, 2, 3}};
    const MyClass* pTemp[]{&temp[0], &temp[1], &temp[2]};
}
const std::vector<MyClass*> MyClass::vec_pre{begin(pTemp), end{pTemp}};

You could also remove the duplication of entry maintainability problem by creating a macro to do that for you.

like image 24
Jonathan Mee Avatar answered Oct 09 '22 12:10

Jonathan Mee