看at this example on compiler explorer:
#include <type_traits>
#include <utility>
template <typename T>
struct A {
template <typename U>
void test(U&& u) noexcept(noexcept(inner_test(std::forward<U>(u)))) {
inner_test(std::forward<U>(u));
}
private:
template <typename U>
void inner_test(U&&) noexcept(
std::is_nothrow_move_constructible_v<std::decay_t<U>> &&
std::is_nothrow_move_assignable_v<std::decay_t<U>>
/* and whatever */) {
// ...
}
};
int main() {
A<int> a;
a.test(3u);
}
有一个类模板和两个功能模板。公共函数只是私有函数的包装,私有函数具有复杂的noexcept规范。
我想让编译器为公共功能推导noexcept规范,以避免 noexcept 代码重复(一旦编写,它已经很糟糕了……)。
Clang和MSVC似乎喜欢该代码,但是GCC拒绝了它,并出现以下错误:
<source>:7:48: error: 'inner_test' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
谁错了谁是对的?
有趣的是:在noexcept(...)中,如果您将“ &&”更改为“ and”,则MSVC将不再编译...
c ++ 17 标记主要是因为我使用了几个在C ++ 14中不可用的“ _v”。