1

I run into trouble if I would create an array of objects like this:

SM sc[8]{{0},{1},{2},{3},{4},{5},{6},{7}};

The constructor for SM is defined as:

SM::SM(int);

Because in c++ Each member is copy-initialized from the corresponding initializer-clause. is given, I have a unsolved problem.

I already read:

Move Constructors and Static Arrays

Initialization of member array objects avoiding move constructor

Move constructor is required even if it is not used. Why?

constexpr array of constexpr objects using move ctor

Yes, all the answers describing very well what is giong on with list initialization but I could not found an idea how to get a static array of objects now.

Is there any work around for that problem? Creating a array of pointers and do a runtime initialization with new or new@ operation requires a lot more runtime a memory space. It is a bit problematic because I am on a AVR 8 bit controller.

Community
  • 1
  • 1
Klaus
  • 24,205
  • 7
  • 58
  • 113
  • You mention "trouble" and "that problem", but never state an actual error. – Ivan Aksamentov - Drop Aug 03 '15 at 16:15
  • The error is: SM::SM(SM&&) is implicitly deleted because the default definition would be ill-formed: SM::SM(SM&&) = default; A long list of errors go through a long list of subclasses which are inherited from my SM class. Maybe I can shrink down my problem to a full example. But this is not helpful, because the fact that the class can't be copy constructed will remain. – Klaus Aug 03 '15 at 16:29
  • [Works for me](http://coliru.stacked-crooked.com/a/f67d4683fe50095d). – T.C. Aug 03 '15 at 16:52

2 Answers2

3

Just some feedback, in an answer due to code snippets:

Adapting the code at the 3rd link to:

#include <iostream>
using namespace std;

struct SM {
    int val;
    SM(int a) : val(a) { cout <<"Constructor val="<<a<<endl;}
    ~SM() { cout << "Destructor val="<<val<<endl; }

    SM(const SM& ) = delete;
    SM(SM&& ) = delete;
};

int main()
{
    SM sc[8] = { {0},{1},{2},{3},{4},{5},{6},{7} };
    return 0;
}

compiling with

g++ -std=c++11

and running, results in:

Constructor val=0
Constructor val=1
Constructor val=2
Constructor val=3
Constructor val=4
Constructor val=5
Constructor val=6
Constructor val=7
Destructor val=7
Destructor val=6
Destructor val=5
Destructor val=4
Destructor val=3
Destructor val=2
Destructor val=1
Destructor val=0

What exactly is the trouble?

Community
  • 1
  • 1
Kenney
  • 9,003
  • 15
  • 21
  • The trouble is quite simple: My class SM is NOT copy nor move constructable. Your class have a default copy and move constructor defined. For my class both are invalid. – Klaus Aug 03 '15 at 16:16
  • What are the constructors of your class then? – Kenney Aug 03 '15 at 16:17
  • @Klaus Like [this](http://coliru.stacked-crooked.com/a/628bc19fe40f0358)? If not, you should really add some more code into your question: [M(C)VE](http://stackoverflow.com/help/mcve) – Ivan Aksamentov - Drop Aug 03 '15 at 16:23
  • I will look for a full compilable example, but this will take a lot time to walk through my code and shrink. It will be at least tomorrow. – Klaus Aug 03 '15 at 16:34
  • @Klaus So [this example](http://coliru.stacked-crooked.com/a/628bc19fe40f0358) is not what you want? – Ivan Aksamentov - Drop Aug 03 '15 at 16:36
  • Sorry, my problem is that I can NOT compile my code. So it simply can not be reproduced in that way. – Klaus Aug 03 '15 at 16:37
  • 1
    @Klaus Did you try to follow Kenney's answer? This code works for non-copyable, non-movable types. – Barry Aug 03 '15 at 18:33
1

"copy-initialized" does not mean "calls a copy constructor."

C++14 8.5/15:

The initialization that occurs in the form

T x = a;

as well as in argument passing, function return, throwing an exception, handling an exception, and aggregate member initialization is called copy-initialization.

Note that an initializer and an initialized object can have different types.

So you can use an initializer list to initialize an array without necessarily invoking any copy constructor.

aschepler
  • 70,891
  • 9
  • 107
  • 161