0

In C++, is there a way to detect at runtime whether a function is operating in a noexcept context or not? For example, I have an imaginary function called noexcept_context():

void a() noexcept
{
   // f set to true
   bool f = noexcept_context() ;
}

void b()
{
    // f set to false as long as b() call stack has no noexcept callers
    bool f = noexcept_context() ;
}

If it is available in C++ 11 all the better. If not in standard C++, is their a MSVC windows special?

maybourne
  • 23
  • 5
  • [Why, though?](https://meta.stackexchange.com/q/66377/317773) – StoryTeller - Unslander Monica Aug 26 '22 at 09:07
  • 3
    There is no such thing as "noexcept context". A `noexcept` function can throw exceptions as much as it wants, directly or by calling other functions, as long as they are all caught inside. Having or not having a `noexcept` caller *somewhere up in the stack* means nothing at all. – n. m. could be an AI Aug 26 '22 at 09:09
  • In function set by _set_invalid_parameter_handler. This gets called by, for example by std::vector out of bounds condition in a noexcept context. If called by a c run time library function it isn't. If you throw an exception when this is called by std::vector, the program aborts. If by a run time library the exception gets caught. – maybourne Aug 26 '22 at 09:14
  • Ok, I see that I would need to know where a thrown exception would get caught. Question answered. – maybourne Aug 26 '22 at 09:31

0 Answers0