테스트, 왜이 코드는 컴파일되지 않습니다재귀 noexcept 사양 g ++ 4.9 및 그 소리 3.4와
namespace {
template<typename T>
constexpr auto f(T && t) noexcept {
return true;
}
template<typename T>
constexpr auto f_helper(T && t) noexcept(noexcept(f(t))) {
return f(t);
}
template<typename T, typename... Ts>
constexpr auto f_helper(T && t, Ts && ... ts) noexcept(noexcept(f(ts...))) {
return f(ts...);
}
template<typename T, typename... Ts>
constexpr auto f(T && t, Ts && ... ts) noexcept(noexcept(f_helper(ts...))) {
return f(ts...);
}
} // namespace
int main() {
f(true, 0, 5u);
}
f_helper 기능을 정의 할 필요가 없습니다, 이 경우에는 decltype 방식으로 지정된 올바른 반환 유형이 있어야합니다.
첫 번째 코드도 1 또는 2 개의 인수로 컴파일되지만 3 회 이상 호출하면 해당 함수를 호출 할 때 오류가 발생합니다. 첫 번째 코드에 대한 연타 에러는 다음 이름 선언
source/main.cpp:9:59: error: call to function 'f' that is neither visible in the template definition nor
found by argument-dependent lookup
constexpr auto f(T && t, Ts && ... ts) noexcept(noexcept(f(ts...))) {
^
source/main.cpp:9:17: note: in instantiation of exception specification for 'f<bool, int, unsigned int>'
requested here
constexpr auto f(T && t, Ts && ... ts) noexcept(noexcept(f(ts...))) {
^
source/main.cpp:16:3: note: in instantiation of function template specialization '<anonymous
namespace>::f<bool, int, unsigned int>' requested here
f(true, 0, 5u);
^
source/main.cpp:9:17: note: 'f' should be declared prior to the call site
constexpr auto f(T && t, Ts && ... ts) noexcept(noexcept(f(ts...))) {
^
1 error generated.
4 개의 매개 변수를 f에 전달하면 도우미가있는 버전은 컴파일되지 않습니다 (http://coliru.stacked-crooked.com/a/126cee2269269e96). –