2

I would like to initialize a constexpr array with a pattern that is generated using variadic template parameters. For simplicity, consider the problem of initializing a constexpr unsigned static array with the sizes of a list of types, say, unsigned, short, char, int, long. How can I do this so that all of the computation is done during compile time? I need the solution to play nice with the C++ type system, so I cannot use macros.

The best I could come up with is shown below, but compilation using g++ -std=c++11 -Wall -Ofast -S (using g++ 4.7) and inspection of the assembly clearly reveals that the values are pushed onto the stack during runtime. Any ideas? and works fine.

Using an array initializer as follows would work if I could somehow tell the expansion n +1 about expansion n.

static constexpr unsigned foo[] = { compute_element<Args>::value... };

Edit: Wait, never mind, I had a brainfart. The line above works fine...

Here is the code answer:

#include <iostream>

template <class... Args>
struct foo
{
    static constexpr unsigned bar[] = { sizeof(Args)... };
};

int main()
{
    std::cout << foo<unsigned, short, char, int, long>::bar[2] << std::endl;
    return 0;
}

Thank you very much for your time!

2
  • 5
    It's encouraged that if you find an answer to your questions to post it as such. :) Commented Jul 16, 2012 at 17:41
  • Ok, I wrote the solution as an answer. Commented Jul 16, 2012 at 20:30

1 Answer 1

5

Here is the answer. Keep in mind that due to limitations in C++, I think that this can only be done in-compile time to create arrays that are of the same size of the variadic template parameter pack.

#include <iostream>

template <class... Args>
struct foo
{
    static constexpr unsigned bar[] = { sizeof(Args)... };
};

int main()
{
    std::cout << foo<unsigned, short, char, int, long>::bar[2] << std::endl;
    return 0;
}
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.