0

I was searching for reasons that prevent my compiler (intel 15) to vectorize my code. The reasons were : loop with early exits cannot be vectorized unless it meets search loop idiom criteria although I did not have any thing like break or exit(0) in it. So I checked all my function with the noexcept operator and it seems that all my template function are likely to throw exception (the operator returns false all the time) althgough I have no try/catch/throw in it. For this reason, I specify the noexcept on my function and now it works but I don't understand why the compiler say that they are likely to throw something ? How can I specify that, by default, they won't throw anything ??

Example:

/*.h*/
template <typename T>
void A(T& a){} /*does nothing*/

/*.cpp*/
#include ".h"
#include "MyClass.h"
int main(){
MyClass C;
noexcept(A(C)); /*Return false !!*/
}

Thanks.

Cory Kramer
  • 114,268
  • 16
  • 167
  • 218
Viridya
  • 640
  • 5
  • 13
  • You seem to have answered your own question - put `noexcept` on functions that don't/won't throw. – Richard Critten Jul 19 '17 at 13:12
  • `noexcept()` will not examine your function definition. That would be intractable. If you want to be noexcept correct, you need to specify it. Just as you would with const correctness. – StoryTeller - Unslander Monica Jul 19 '17 at 13:17
  • Also note, that if the function marked as `noexcept` will actually throw despite the specification, the exception cannot be caught (via try-catch) and the `std::terminate` will always be called (even if there is a try-catch block for the exception). – EmDroid Jul 19 '17 at 13:39

1 Answers1

0

If you look at the documentation for the noexcept operator (emphasis mine)

The result is false if the expression contains at least one of the following potentially evaluated constructs:

  • call to any type of function that does not have non-throwing exception specification, unless it is a constant expression.
  • throw expression.
  • dynamic_cast expression when the target type is a reference type, and conversion needs a run time check
  • typeid expression when argument type is polymorphic class type

In all other cases the result is true.

So as you noticed, the way to have your template function evaluate noexcept as true is simply to declare it as such

template <typename T>
void A(T& a) noexcept
{
}
Community
  • 1
  • 1
Cory Kramer
  • 114,268
  • 16
  • 167
  • 218