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.

15
  • 3
    "I do not want to use std::array" Why? It does exactly what you need. Commented Oct 17, 2019 at 12:11
  • 1
    Defines in standard header? If there compiler has any macros that are not reserved 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. Commented Oct 17, 2019 at 12:18
  • 1
    @lightxbulb That sounds wrong/unlikely/misunderstood to me. What defines? What "out of namespace" stuff? Commented Oct 17, 2019 at 12:19
  • 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. Commented Oct 17, 2019 at 12:22
  • 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 <array> across GCC, clang, xcode, Visual studio, on MacOS, Linux, Windows, AiX and FreeBSD. But sure, do your thing.. Commented Oct 17, 2019 at 12:25

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

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.