1

I want to remove duplicates from a constexpr std::array at compile time.

Basically, I'm trying to implement a consteval function, which returns a copy of the original array without duplicates:

consteval auto unique(std::array<...>) -> std::array<...>

I know std::sort is constexpr, so I thought I could first sort it first, with the intention of using std::unique next, but then I can't quite figure out how to make that work, there might be a better way than that.

Giovanni Funchal
  • 8,934
  • 13
  • 61
  • 110
  • Why do you have to do it at compile-time? What is the original problem you need to solve? What is the contents of your array? How many elements are there in it? Is `std::array` really the correct container to begin with? – Some programmer dude Jul 18 '22 at 12:30
  • Please provide full specification of `unique` possibly by using unit tests. What exactly should happen when duplicates are found? Should return type change too? – Marek R Jul 18 '22 at 12:37
  • Sorry - maybe my dupe hammer was wielded a bit hastily. Linked dupe only checks *if* there are dupes, not how to remove them. – Adrian Mole Jul 18 '22 at 12:37
  • also `consteval` suggest C++20. Is this a minimum C++ standard? Please clearly specify that (add tag). – Marek R Jul 18 '22 at 12:39
  • ...feel free to hammer open or ping me to do so, if the answers in the linked target don't help. – Adrian Mole Jul 18 '22 at 12:40
  • I doubt if you can declare the size of the array in the return value. I have a solution, but it is not satisfying, because I also returned the new length which has to be taken into account for the real result, but it is done as consteval: https://coliru.stacked-crooked.com/a/fc78a43bea92f907 – stefaanv Jul 18 '22 at 14:17

0 Answers0