1

Is there a compile time expression to copy an array in an object constructor? What does the default constructor use? I want something like this:

struct A
{
    int arr[100];
    // I want something like this:
    A(const A& arg) : arr{arg.arr...} {}
    // what I use at the moment (a compile time loop):
    A(const A& arg)
    {
        static_for<0, N>([&](auto i) { arr[i] = arg.arr[i]; });
    }
};

I do not want to use std::array, and I have some debug info in the copy ctor, so I cannot rely on the default one.

lightxbulb
  • 1,251
  • 12
  • 29
  • It's not possible to initialize C-style array from another array in member initializer list. If you really don't want `std::array` (why?), then your only option is to call [`std::copy_n`](https://en.cppreference.com/w/cpp/algorithm/copy_n) (or `std::memcpy` if the type is POD) in constructor body. – Yksisarvinen Oct 17 '19 at 12:11
  • 3
    "_I do not want to use `std::array`_" Why? It does exactly what you need. – Algirdas Preidžius Oct 17 '19 at 12:11
  • @AlgirdasPreidžius Because it comes attached with a bunch of other headers, and defines out of any namespace. – lightxbulb Oct 17 '19 at 12:15
  • @Yksisarvinen `std::copy_n` is not strictly compile-time - it may get optimized to be, or it may not. The `static_for` that I have can do things that the `std::...` algorithms cannot do at compile time. – lightxbulb Oct 17 '19 at 12:16
  • Why are you using a C-style array in C++? What's wrong with [std::array](https://en.cppreference.com/w/cpp/container/array)? – Jesper Juhl Oct 17 '19 at 12:16
  • @JesperJuhl `Because it comes attached with a bunch of other headers, and defines out of any namespace.` – lightxbulb Oct 17 '19 at 12:17
  • 1
    Defines in standard header? If there compiler has any macros that are not [reserved identifiers](https://en.cppreference.com/w/cpp/language/identifiers), then you should file a bug for that compiler. If you are using reserved identifiers in your code, stop. It's UB to use reserved identifiers. – Yksisarvinen Oct 17 '19 at 12:18
  • @Yksisarvinen I have had collisions with stuff that was defined outside of any namespace previously exactly from including `std::array`. Can't remember what exactly, but I can tell you I did not enjoy it. – lightxbulb Oct 17 '19 at 12:19
  • 1
    @lightxbulb That sounds wrong/unlikely/misunderstood to me. What defines? What "out of namespace" stuff? – Jesper Juhl Oct 17 '19 at 12:19
  • @JesperJuhl Something that was obviously not put in `std::`. You can find many of those if you go through `` and its included headers. I can't remember what it was last time, but it was certainly not a pleasant experience. – lightxbulb Oct 17 '19 at 12:22
  • 1
    Preprocessor macro cannot be in any namespace, they are resolved without regard to namespaces. It's a simple "find-and-replace" mechanism. We understand that it's not nice experience to find such thing, but it's either a very serious bug in your compiler or an issue in your code. And really, I never heard about a compiler which would define macros not as reserved identifiers. If any of your names fits the criteria of reserved identifiers (linked above), it's your fault, not standard header's. – Yksisarvinen Oct 17 '19 at 12:22
  • @Yksisarvinen I am referring to definitions and declarations by defines. Not to preprocessor defines, though having those leak into my code isn't nice either. – lightxbulb Oct 17 '19 at 12:25
  • 2
    @lightxbulb Fine, whatever. Keep using primitive things in place of nice things due to handwavy reasons about namespaces. That's your choice. I've, personally, never had a problem with `` across GCC, clang, xcode, Visual studio, on MacOS, Linux, Windows, AiX and FreeBSD. But sure, do your thing.. – Jesper Juhl Oct 17 '19 at 12:25
  • @JesperJuhl Good for you I guess. There are people that actually have problems with collisions, whether you have had them or not - I am certainly not the only one - I know such people. This is way beyond what I asked and pretty much a personal preference argument at this point, so I'll leave it at that. It's the XY problem reversed. – lightxbulb Oct 17 '19 at 12:28
  • 1
    @lightxbulb I will reiterate, what Jesper stated: If you use reserved names, the collisions, is the problem of **your** code, not the problem of STL. You mustn't define something, having the same name, as a reserved name. For instance: names [staring with an underscore](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) are reserved, under some cases. It's like writing broken code, but blaming everyone else, except yourself, for it not working. – Algirdas Preidžius Oct 17 '19 at 12:57

1 Answers1

5

AFAIK there is only the loop based solution for now if I understand correctly how you framed the question - at least as of now

From there is constexpr version of copy_n

Other users should just use a proper container: std::array

darune
  • 10,480
  • 2
  • 24
  • 62