https://gcc.gnu.org/bugzilla/show_bug.cgi?id=115987
--- Comment #8 from Dan Urosu <durosu at yahoo dot com> --- Why does not warn for unwarp_1? The mere addition of an unused template parameter triggers this warning. Also "decorating the unwrap_2 function to tell the compiler not to warn there." does not work if we have a macro calling unwrap_2. (Which we would use in the real code) #include <iostream> #include <optional> // Compile: gcc -std=c++17 -Wall -Wextra -Werror -lstdc++ main_unwrap.cpp -o main_unwrap /** 'unwrap_1' and 'unwrap_2' are functionally identical. By introducing an additional template parameter, the code fails to compile, when unwrap_2 is used. The code should not give errors because the lifetime of the returned reference is tied to the lifetime of the 'wrapper' object. */ template <typename T> struct Wrapper { const T& value() const { return val.value(); } T& value() { return val.value(); } std::optional<T> val; }; template <typename T> const T& unwrap_1(const Wrapper<T>& r) { return r.value(); } #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wdangling-reference" template <typename T, typename FUNC> const T& unwrap_2(const Wrapper<T>& r, FUNC&&) { // note FUNC is not even used return r.value(); } #define UNWRAP_2(x, y) unwrap(x, y) #pragma GCC diagnostic pop #define WITH_FUNC_PARAM int main(int, char**) { const Wrapper<int> w{1234}; #ifndef WITH_FUNC_PARAM const auto& u = unwrap_1(w); #else const auto& u = UNWRAP_2(w, 1); // This fails with error: possibly dangling reference to a temporary [-Werror=dangling-reference] #endif printf("Unwrapped value : %d\n", u); return 0; }