fork download
  1. template<int x, int y>
  2. int div_round_up() {
  3. static_assert(y > 0 && x >= 0, "This function only works for positive divisor and non-negative dividend");
  4. if (x == 0)
  5. return 0;
  6. return (x - 1) / y + 1;
  7. }
  8.  
  9. int main() {
  10. auto a = div_round_up<3, 4>();
  11. auto b = div_round_up<2, -1>(); // Fails the static assert
  12. }
Compilation error #stdin compilation error #stdout 0s 3092KB
stdin
Standard input is empty
compilation info
prog.cpp: In instantiation of 'int div_round_up() [with int x = 2; int y = -1]':
prog.cpp:11:31:   required from here
prog.cpp:3:5: error: static assertion failed: This function only works for positive divisor and non-negative dividend
     static_assert(y > 0 && x >= 0, "This function only works for positive divisor and non-negative dividend");
     ^
stdout
Standard output is empty