Is OK to throw an exception when 0 is passed to allocate method?
Thank you.
P.S.
If n == 0, the return value is
unspecified.
Does it mean that allocate shouldn’t throw an exception? I am inclined to think that if throwing wasn’t allowed for n==0, then the standard would clearly spell it out.
All the standard has to say (§20.1.5/2) is that given
T– any typeX– an Allocator class for typeTa– a value of typeX&n– a value of typeX::size_type,the return value of the expression
a.allocate(n)is unspecified ifnequals 0.One one hand, given that
X::allocatedoesn’t have any mandated exception specification and explicitly may throwstd::bad_alloc, I don’t see why it couldn’t throw some other type of exception as well. On the other hand, the wording specifically calls out the condition whereinnequals zero and directly implies that there is a return value, meaning you shouldn’t throw. I think it may be open to some interpretation, but personally I’d side with the latter and consider it an unexceptional code-path.