2

I wonder it it is possible to sort numbers during compilation? I mean something like that:

template<int...>
void sort(){
...
}

And:

sort<2,4,5,13,453>();

And I don't ask of solution or something like that. Please give me a hint or reference me.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
Gilgamesz
  • 4,727
  • 3
  • 28
  • 63
  • 5
    Did you try googling "sort variadic list of ints c++"? It should have brought up [Quick sort at compilation time using C++11 variadic templates](http://stackoverflow.com/questions/7188182/quick-sort-at-compilation-time-using-c11-variadic-templates). – NathanOliver Jan 25 '16 at 18:34
  • 1
    Note that this problem is easy in C++14 with relaxed constexpr, and more annoying in C++11. And the answer is yes, as TMP is turing-complete. Plus I've done it before. – Yakk - Adam Nevraumont Jan 25 '16 at 19:07
  • 1
    TMP is turning complete, you can calculate anything – Motti Jan 25 '16 at 19:07

1 Answers1

3

Since C++ template system is known to be turing-complete, you can in principle compute everything that is computable at compile time. That includes sorting algorithms.

Dirk Herrmann
  • 5,550
  • 1
  • 21
  • 47