Date: Fri, 4 Mar 2022 12:54:03 +0100
Currently when computing things at compile time one sometimes faces a
problem when trying to output the result to a constexpr variable.
This happens when the result is data structure without a fixed size that
utilizes dynamic memory allocation, which in itself is not a
constant expression (for example a vector). Today the result can be
converted to a more appropriate fixed sized structure like this:
constexpr std::vector<int> compute_vector();
constexpr std::size_t extract_size() { return compute_vector().size(); }
constexpr auto as_array() {
auto vec = compute_vector();
std::array<int, extract_size()> arr;
std::copy(vec.begin(), vec.end(), arr.begin());
return arr;
}
But this somewhat awkwardly involves doing the computation twice. The only
way around this is to copy the data to a large intermediate array
then copy it again into an array of the right size. Neither of these
solutions is really ideal and that is what this proposal aims to solve.
For this I want to introduce a new kind of variable that can be declared
inside an immediate function. With this the above example could look like:
constexpr std::vector<int> compute_vector();
consteval auto as_array() {
consteval auto vec = compute_vector(); // example syntax
std::array<int, vec.size()> arr;
std::copy(vec.begin(), vec.end(), arr.begin());
return arr;
}
The requirements for this type of variable should conceptually be similar
to those for constexpr variables (see 9.2.6.10 and 7.7.11) except that:
- Targets of pointers and references don't have to have static storage
duration. In particular pointers to dynamically allocated memory are
allowed.
- Memory allocations performed in the initialization can (and must) be
deallocated in the destructor.
- And (i think) pointers to immediate functions can also be allowed,
since this type of variable only exists inside immediate functions.
Although this
isn't required for the aim of this proposal.
To my reading this lifts all the requirements of 7.7.11 so that any core
constant expression with the modified allocation requirement would be
allowed.
Like with constexpr variables these new variables should also be implied to
be const. And I propose for consistency they should be allowed to be
declared in any immediate function context as by 7.7.13 (i.e. also inside a
if consteval statement).
Looking forward to your feedback
Torben
problem when trying to output the result to a constexpr variable.
This happens when the result is data structure without a fixed size that
utilizes dynamic memory allocation, which in itself is not a
constant expression (for example a vector). Today the result can be
converted to a more appropriate fixed sized structure like this:
constexpr std::vector<int> compute_vector();
constexpr std::size_t extract_size() { return compute_vector().size(); }
constexpr auto as_array() {
auto vec = compute_vector();
std::array<int, extract_size()> arr;
std::copy(vec.begin(), vec.end(), arr.begin());
return arr;
}
But this somewhat awkwardly involves doing the computation twice. The only
way around this is to copy the data to a large intermediate array
then copy it again into an array of the right size. Neither of these
solutions is really ideal and that is what this proposal aims to solve.
For this I want to introduce a new kind of variable that can be declared
inside an immediate function. With this the above example could look like:
constexpr std::vector<int> compute_vector();
consteval auto as_array() {
consteval auto vec = compute_vector(); // example syntax
std::array<int, vec.size()> arr;
std::copy(vec.begin(), vec.end(), arr.begin());
return arr;
}
The requirements for this type of variable should conceptually be similar
to those for constexpr variables (see 9.2.6.10 and 7.7.11) except that:
- Targets of pointers and references don't have to have static storage
duration. In particular pointers to dynamically allocated memory are
allowed.
- Memory allocations performed in the initialization can (and must) be
deallocated in the destructor.
- And (i think) pointers to immediate functions can also be allowed,
since this type of variable only exists inside immediate functions.
Although this
isn't required for the aim of this proposal.
To my reading this lifts all the requirements of 7.7.11 so that any core
constant expression with the modified allocation requirement would be
allowed.
Like with constexpr variables these new variables should also be implied to
be const. And I propose for consistency they should be allowed to be
declared in any immediate function context as by 7.7.13 (i.e. also inside a
if consteval statement).
Looking forward to your feedback
Torben
Received on 2022-03-04 11:54:15