Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow manipulation of etl::array in constexpr function #718

Conversation

Vitmark97
Copy link
Contributor

This is mainly usefull for creating constexpr arrays in C++14 and up.

Example:

template <size_t SIZE>
constexpr etl::array<int, SIZE> x_squared(int start_x) {
  etl::array<int, SIZE> out{};
  etl::iota(out.begin(), out.end(), start_x);
  for (int& elem : out) {
    elem = elem * elem;
  }
  return out;
}

// Contains [25,16,9,4,1,0,1,4,9,16,25]
constexpr etl::array compile_time_array = x_squared<11>(-5);

// Contains [35,26,19,14,11,10,11,14,19,26,35]
constexpr etl::array lambda_generated_array = []() constexpr {
  etl::array out{compile_time_array};
  for (size_t i = 0; i < out.size(); ++i) {
    out[i] = out[i] + 10;
  }
  return out;
}();

@jwellbelove jwellbelove changed the base branch from master to pull-request/#718-Allow-manipulation-of-etl--array-in-constexpr-function June 26, 2023 15:32
@jwellbelove jwellbelove merged commit f2ca268 into ETLCPP:pull-request/#718-Allow-manipulation-of-etl--array-in-constexpr-function Jun 26, 2023
58 checks passed
jwellbelove added a commit that referenced this pull request Jul 1, 2023
…onstexpr-function' into development

# Conflicts:
#	test/test_optional.cpp
jwellbelove pushed a commit that referenced this pull request Jul 4, 2023
* iota can be contexpr

* Make it possible to iterate over etl::array in constexpr functions
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants