0

The following class is meant to store a single bool and an array of 100 integers. I want a constructor that takes as parameters a single bool and a 100-element array (which are then copied into the object). The obvious way to write this is the following

class Info {

private:
    const int contents[100];
    const bool symmetric;
public:
    Info (const int contents[100], bool symmetric) : contents(contents), symmetric(symmetric) {}
    // ...
};

But unfortunately this does not compile, the contents argument in the member initialiser list (the third contentson that line) seems to be regarded as a pointer.

Can I get this to work or is this a C++ limitation?

  • I do not want to use std::array or std::vector for space and efficiency reasons. [edited] Mine is a C++ syntax question and not about whether std::array is efficient or not. The answer may be useful in related situations that do not allow the use of std::array. [end edited]
  • Initializing in the constructor body does not work, because contents is const.

I am using C++14 with clang (CLion in linux) and come from the Java world.

kcoolsae
  • 1
  • 1
  • 5
    Why do you believe `std::array` is not efficient? `std::array` is a thin wrapper around the C-style array that has no overhead (see https://stackoverflow.com/questions/30263303/stdarray-vs-array-performance) and brings you the exact value semantics that you expect. – stiar Feb 01 '21 at 13:17
  • Does this answer your question? [initialize a const array in a class initializer in C++](https://stackoverflow.com/questions/161790/initialize-a-const-array-in-a-class-initializer-in-c) – BartoszKP Feb 01 '21 at 13:18
  • 2
    Can you show an example which shows exactly how a `std::array` will take more space and/or be less efficient than a plain array? – Sam Varshavchik Feb 01 '21 at 13:19
  • @BartoszKP It does not answer my question because I want to initialize the array member with a copy of an existing array at runtime, not necessarily constant, and of length 100, so I do not want an initialiser list of 100 individual letters. – kcoolsae Feb 01 '21 at 13:39
  • This is a C++ syntax question and not about whether `std::array` is efficient or not. Anyhow, in the real application I need this for, speed and memory usage is of the essence. – kcoolsae Feb 01 '21 at 13:41
  • 2
    Yes, and `std::array` brings absolutely no speed/memory overhead whatsoever. It is precisely fixed size C array + value semantics, nothing less, nothing more. If you want to do it the C++ way, you use `std::array`. Or you can reimplement `std::array` yourself if you do not trust the standard library. If you want to do it the C way, you would have to use `memcpy` then to copy the data behind the pointer. – stiar Feb 01 '21 at 14:01
  • @kcoolsae It does answer exactly the same question. The fact that in the example there is a constant given in the initializer list is irrelevant. The point is that the class member is constant (in your question and in the linked dupe) and that it's impossible (in your question in and in the linked dupe) and that you should understand that `std::array` has been created for this purpose (in your question and in the linked dupe). – BartoszKP Feb 01 '21 at 14:12
  • I will have a better look at `std::array`. I did not realize that internally it was nothing other than a C array. Thanks for pointing that out. – kcoolsae Feb 01 '21 at 18:50

1 Answers1

0

There is also another way other than std::array, but you should use templates and use variadic parameter feature.

Lets assume that the number of elements is amount then first declare your class like :

#include <utility>

template<size_t amount, typename = std::make_index_sequence<amount> >
class Info;

then define your class as the following:

template<size_t amount, size_t ... _Idx>
class Info<amount, std::integer_sequence<size_t, _Idx...> > {
private:
    const int contents[amount];
    const bool symmetric;
public:
    Info (const int contents_[amount], bool symmetric_) : contents{contents_[_Idx]...}, symmetric(symmetric_) {}
};

now you use your class for any amount without any problem:

    const int preinfo[3] = {1,2,3};
    Info<3> minfo(preinfo, true);

This method will make it very fast but please note that nothing is free and using this code will make your code to produce a bigger binary after compilation!

newbie
  • 415
  • 3
  • 11