3

I have some code like this:

class MyObject {
  private:
    int x;
  public:
    MyObject(int x) : x(x) {}
};

I want to initialize 5 MyObject instances. I know I can do this with g++ 8.3.0.

MyObject obj_array[5]{1, 2, 3, 4, 5};

However, I'm unable to do this:

const int myInts[5] = {1, 2, 3, 4, 5};
MyObject obj_array[5](myInts);

Is there any way to make the second initialization method (initializing array of objects with initialization list constructors using const integer array) work? The rub is that we have a special compiler framework that doesn't allow dynamic memory or most STL datatypes such as vector.

3
  • Just use std::array (or boost::array, or your own variant). It doesn't allocate anything dynamically. Do you require exactly the initialization syntax that you are asking for? Commented Dec 10, 2019 at 22:14
  • Can you use std::array's? Also, what language standard can you use (03,11,14,17)? Commented Dec 10, 2019 at 22:14
  • std::array also not allowed. The (highly proprietary) compiler allows a subset of C++11, but no dynamic memory, no recursion, no STL datatypes, no transcendental functions, etc. Commented Dec 10, 2019 at 22:35

3 Answers 3

2

If we're doing ugly solutions, here's one with scalability:

#define LIST { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
const int myInts[] = LIST;
MyObject obj_array[] = LIST;
#undef LIST
Sign up to request clarification or add additional context in comments.

1 Comment

This is actually a little better than @selbie's original solution. Thanks for the code snippet.
2

Not pretty, but this seems to work:

const int myInts[5] = { 1, 2, 3, 4, 5 };
MyObject obj_array[5] = {myInts[0], myInts[1], myInts[2], myInts[3], myInts[4]};

2 Comments

Ah, this is great! This is pretty close to what I was looking for.
The repetition could be hidden in a Boost.Preprocessor trick, too.
1

Not really, since when a raw array type is initialized with fewer than its count of expressions, objects get default-initialized.

But one thing you could do would be to create your own array-like class template:

#include <cstddef>
#include <type_traits>
#include <initializer_list>
#include <utility>
#include <iterator>

template <std::size_t N>
class MyObjectArray
{
public:
    using array_type = MyObject[N];
    template <typename T, typename Enable =
        std::enable_if_t<std::is_convertible_v<T, int>>>
    MyObjectArray(T (&arr)[N]);

    template <typename T = int, typename Enable =
        std::enable_if_t<std::is_convertible_v<T, int>>>
    MyObjectArray(std::initializer_list<T> il);

    template <typename InputIter, typename Enable =
        std::enable_if_t<std::is_convertible_v<
            typename std::iterator_traits<InputIter>::reference, int>>>
    MyObjectArray(InputIter start, InputIter end);

    operator array_type& () noexcept { return m_array; }
    operator const array_type& () const noexcept { return m_array; }

private:
    template <std::size_t... I, typename InputIter>
    MyObjectArray(std::index_sequence<I...>, InputIter iter)
        : m_array{ (static_cast<void>(I), *iter++) ... } {}

    static std::make_index_sequence<N> check_count(std::size_t n) {
        if (n != N)
            throw std::invalid_argument(
                "Incorrect number of elements initializing MyObjectArray");
        // Or if exceptions not supported, some other error reporting
        // mechanism and abort.
        return {};
    }

    MyObject m_array[N];
};

template <std::size_t N>
template <typename T, typename Enable>
MyObjectArray<N>::MyObjectArray(T (&arr)[N])
    : MyObjectArray(std::make_index_sequence<N>{}, arr) {}

template <std::size_t N>
template <typename T, typename Enable>
MyObjectArray<N>::MyObjectArray(std::initializer_list<T> il)
    : MyObjectArray(check_count(il.size()), il.begin()) {}

template <std::size_t N>
template <typename InputIter, typename Enable>
MyObjectArray<N>::MyObjectArray(InputIter start, InputIter end)
    : MyObjectArray(check_count(std::distance(start, end)), start) {}

This can be used like:

int main()
{
    const int myInts[5] = {1, 2, 3, 4, 5};
    MyObjectArray obj_array(myInts);

    MyObjectArray<5> a2 = {3, 4, 5, 6, 7};
}

See the working code on coliru.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.