5

The current C++ standard enables compilers to look into the function body for information to complete a function's signature:

template <typename T>
inline auto dereference(T const& pointer) {
    return *pointer;
}

And I wonder if the compiler could automatically declare whether a function is nothrow guaranteed if the definition can be seen, maybe with syntax like this:

void func() noexcept(auto) {
    … // If all operations in the function are noexcept, the
      // function can be automatically declared noexcept
}

If that is possible, it would saves much trouble in writing the noexcept declaration, especially when writing templates, where functions are usually followed by their definitions on their first appearence:

// The noexcept declaration can be even longer than the function body
template <typename T>
void func(T& value) noexcept(
    noexcept(value.member_1()) &&
    noexcept(value.member_2()) &&
    noexcept(value.member_3())
) {
    value.member_1();
    value.member_2();
    value.member_3();
}

// Things will become much easier if the C++ standard support this feature
template <typename T>
void func(T& value) noexcept(auto) {
    value.member_1();
    value.member_2();
    value.member_3();
}

So, is this feature possible to enter a future version of ISO/IEC 14882?

iDingDong
  • 447
  • 2
  • 11
  • Upvoted because interesting, but maybe primarily opinion-based or too broad in case of negative response with references to the standard. – skypjack Nov 05 '15 at 16:48
  • [p0133r0: Putting noexcept(auto) on hold, again](http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2015/p0133r0.html) – cpplearner Nov 06 '15 at 02:16
  • @skypjack So what should I do with this question – iDingDong Nov 06 '15 at 02:18
  • Let it be, as I said, it looks interesting anyway. – skypjack Nov 06 '15 at 06:44
  • Possible duplicate of [Is there an automatic noexcept specifier?](https://stackoverflow.com/questions/30456801/is-there-an-automatic-noexcept-specifier) – jotik Sep 10 '18 at 08:30

0 Answers0