0

I appreciate I am being somewhat vague about what is exactly my issue, but I think that the fundamental question is clear. Please bear with me for a moment.

In brief, I have a static constexpr array of points which are used to find certain bounds I need to use. These bounds depend only on the array, so they can be precomputed. However, we want to be able to change these points and it is a pain to go and change every value every time we try to test something.

For example, let's say that I have the following setup:

The static constexpr array is

static constexpr double CHECK_POINTS[7] = { -1.5, -1.0, -0.5, 0.0, -0.5, 1.0, 1.5 };

and then in a function I'm calling, I have the following block of code:

std::vector<double> bounds = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
for(int i=0; i<bounds.size(); i++)
{
    bounds[i] = std::exp(CHECK_POINTS[i]);
}

Clearly, the values of bounds can be computed during compilation. Is there anyway I can make gcc do that?

EDIT: The vector in my code block is not essential, an array will do.

tst
  • 1,117
  • 2
  • 10
  • 21
  • 2
    write a constexpr function. – apple apple Aug 12 '22 at 15:27
  • @appleapple just that? It doesn't need any special treatment? – tst Aug 12 '22 at 15:28
  • 2
    @tst It does. You need to make the function `constexpr`. – Jason Aug 12 '22 at 15:29
  • There are some rules in `constexpr` functions, not every syntax is legal. But in C++17, you can have advanced power including loops. If the compiler accepts to compile, it should work. – prapin Aug 12 '22 at 15:30
  • The rules are explained here : https://en.cppreference.com/w/cpp/language/constant_expression – Pepijn Kramer Aug 12 '22 at 15:30
  • @appleapple [`std::exp` isn't required to be `constexpr`](https://en.cppreference.com/w/cpp/numeric/math/exp). In fact [most](https://stackoverflow.com/q/42189190/995714) [functions](https://stackoverflow.com/q/17347935/995714) in [`cmath`](https://stackoverflow.com/q/21303498/995714) [aren't `constexpr`](https://stackoverflow.com/q/50477974/995714) and the proposal to make them `constexpr` hasn't been approved yet although some implementations do make them constexpr themselves – phuclv Aug 12 '22 at 15:31
  • @phuclv I believe that's not problem. I'm more concerned constexpr construct of vector. – apple apple Aug 12 '22 at 15:32
  • 1
    @JasonLiam it isn't simple like that. You can't call a non constexpr function like `std::exp` in a constexpr function – phuclv Aug 12 '22 at 15:32
  • @appleapple I wouldn't care about that first. It's easy to construct a fixed list of bounds but only after you have a constexpr way to calculate the values – phuclv Aug 12 '22 at 15:33
  • @phuclv it's fine with array https://godbolt.org/z/be57xsGjf – apple apple Aug 12 '22 at 15:35
  • @appleapple as I said, that's a [gcc extension](https://stackoverflow.com/q/70523217/995714). It [wont' work in other compilers](https://godbolt.org/z/j4jY9saG4). And FYI [there's `constexpr std::vector` in C++20](https://stackoverflow.com/a/52801072/995714) so that won't be a problem. The only issue here is the constexpr std::exp – phuclv Aug 12 '22 at 15:38
  • @phuclv yes, but the `constexpr std::vector` doesn't seems to works here anyway https://godbolt.org/z/E8fY33nYs – apple apple Aug 12 '22 at 15:43
  • @phuclv til it's gcc extension, thanks. – apple apple Aug 12 '22 at 15:47

1 Answers1

2
static constexpr double CHECK_POINTS[7] = { -1.5, -1.0, -0.5, 0.0, -0.5, 1.0, 1.5 };

static constexpr auto vec = [](){
    std::array bounds = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
    for(int i=0; i<bounds.size(); i++)
    {
        bounds[i] = std::exp(CHECK_POINTS[i]);
    }
    return bounds;
}();

it's would compile fine with gcc https://godbolt.org/z/x5a9q9M1d


(constexpr std::exp is an gcc extension, thanks to @phuclv to point out)

apple apple
  • 10,292
  • 2
  • 16
  • 36