I am having some trouble wrapping my head around noexcept.
template <int SIZE>
int pop(int idx) noexcept(noexcept(SIZE > 0)) // this is what I dont understand
{
if (idx <= 0)
throw std::out_of_range("My array doesnt go that high");
return idx;
}
This is just a simple function, but you see how it only throws an exception when idx <= 0, I dont understand. So in my specification, noexcept(idx > 0), I am trying to tell the compilier this function ONLY throws no exceptions if idx > 0. Am I doing this right?
Any help is appreciated, I hope I am explaining it right. Just some simple explanation would be great.
Actually the
noexeptspecification expects a constant expression, not a runtime expression. You have used thenoexceptspecificatiom together with thenoexceptoperator.noexcept(idx >0)returnstrueas comparing two integers does not throw, and you are using thattrueas the argument to thenoexceptspecification telling the compiler your function never throws. The declarationsays this function does not throw as long as
idx > 0does not throw, which is always the case for anint.Update: Now you've changed the code in the question so that
idxis a non-type template argument, however the same reasoning applies. Comparingints never throws.What you seem to be trying to do cannot be done in C++. That is, specify whether a function throws or not based on its runtime arguments.