I am trying to use the return value from a consteval function UniqueSize() inside another consteval function UniqueArra to declare an array, but this results in a compiler error saying that UniqueSize() is not a constant expression.
However, if I use UniqueSize() inside main() and use its return value to declare an array the compiler just accepts it.
Why is this happening? Can someone please explain the difference of constant expressions in these contexts?
See the code below. https://godbolt.org/z/nd18WE8M5
template <typename... Args>
consteval auto UniqueSize(Args&&... args)
{
std::vector v{std::forward<Args>(args)...};
std::sort(v.begin(), v.end());
auto newEnd = std::unique(v.begin(), v.end());
return std::distance(v.begin(), newEnd);
}
template <typename... Args>
consteval auto UniqueArray(Args&&... args)
{
// Calling UniqueSize to determine the size of the array returned
constexpr auto sz = UniqueSize(std::forward<Args>(args)...);
std::array<int, sz> arr{};
std::vector v{std::forward<Args>(args)...};
std::sort(v.begin(), v.end());
auto newEnd = std::unique(v.begin(), v.end());
std::copy(v.begin(), v.end(), arr.begin());
return arr;
}
int main()
{
// This compiles fine
constexpr auto sz = UniqueSize(2, 3, 4, 4, 5, 5);
static_assert(sz == 4);
std::array<int, sz> array{};
// This gives an error complaining that `sz` inside UniqueArray is not a constant expression.
// auto arr = UniqueArray(2, 3, 4, 4, 5, 5);
}
consteval/constexprfunctions.template <auto... Args> consteval auto UniqueArray()would be possible.