Testing with g ++ 4.9 and clang 3.4 why this code does not compile:
namespace { template<typename T> constexpr auto f(T && t) noexcept { return true; } template<typename T, typename... Ts> constexpr auto f(T && t, Ts && ... ts) noexcept(noexcept(f(ts...))) { return f(ts...); } }
But this code does:
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...); } }
The f_helper function does not have to be defined; it must have the correct return type specified in this case by decltype.
The first code also compiles for 1 or 2 arguments, but as soon as I try to call it with 3 or more, I get errors regarding inappropriate functions to call. The clang error for the first code:
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.
source share