I ran into this by accident, while playing around with the noexcept operator when trying to implement my own version of std::visit for educational purposes. This is the code with only the relevant parts:
#include <iostream>
template<typename... Ts>
struct visitor : Ts... { using Ts::operator()...; };
template<typename... Ts>
visitor(Ts...) -> visitor<Ts...>;
int main() {
auto vis1 = visitor {
[](int s) {
},
[](double s) {
}
};
constexpr auto isNoExcept = noexcept(vis1);
std::cout << std::boolalpha << isNoExcept << '\n';
return 0;
}
This always outputs true for me (gcc and clang).
I have 2 questions:
- How can the
noexceptoperator even be applied when there are multipleoperator()s, since some might benoexcept, while others are not? How can there be one answer? What exactly happens here? - Why does this return
true, even though none of the lambdas are declarednoexcept?