Reputation: 303527
Consider this example, from bug 80985:
template <class Func>
void call(Func f)
{
f();
}
void func() noexcept { }
int main()
{
call(func);
}
Compiling this with all warnings enabled, as you do, yields:
$ g++ -std=c++14 -Wall foo.cxx
foo.cxx:2:6: warning: mangled name for ‘void call(Func) [with Func = void (*)() noexcept]’ will change in C++17 because the exception specification is part of a function type [-Wnoexcept-type]
void call(Func f)
^~~~
What exactly am I supposed to do with this warning? What is the fix?
Upvotes: 35
Views: 7793
Reputation: 6270
In addition to what is already said, I have found another way to get rid of this warning in GCC 7. Apparently, GCC generates this warning if and only if the first instantiation of call()
involves noexcept
. So a solution would be first to instantiate call()
with a not noexcept
function.
This trick would also do:
using dummy = decltype(call(std::declval<void(*)()>()));
P.S. GCC 8.2.1 does not report a warning in this case.
Upvotes: 1
Reputation: 867
I'm upvoting Ross's answer for the call<void (*)()>(func)
solution. It explicitly tells the compiler that you want the template instantiated for a non-noexcept
function type, and guarantees that your code will operate exactly the same in C++17 as it did in C++14.
More alternatives are:
(1) Wrap the noexcept
function in a lambda (which isn't noexcept
):
template <class Func>
void call(Func f)
{
f();
}
void func() noexcept { }
int main()
{
call([]() { func(); });
}
(2) Create a separate wrapper function without noexcept
. This is more typing initially, but if you have multiple call sites it could save typing overall. This is what I ended up doing in the code that originally prompted me to file the GCC bug.
Upvotes: 2
Reputation: 39621
There's several things you can do about the warning message.
Disable it with -Wno-noexcept-type
. In many projects the warning message is unhelpful because there's no chance the resulting object will be linked with an another object that expects it to use GCC's C++17 name mangling. If you're not compiling with different -std=
settings and you're not building a static or shared library where the offending function is part of its public interface then the warning message can safely disabled.
Compile all your code with -std=c++17
. The warning message will go away as the function will use the new mangled name.
Make the function static
. Since the function can no longer be referenced by another object file using a different mangling for the function the warning message will not be displayed. The function definition will have to be included in all compilation units that use it, but for template functions like in your example this is common anyways. Also this won't work for member functions were static
means something else.
When calling a function template specify the template parameter explicitly giving a compatible function pointer type that doesn't have the exception specification. For example call<void (*)()>(func)
. You should also be able to use cast to do this as well, but GCC 7.2.0 still generates a warning even though using -std=c++17
doesn't change the mangling.
When the function isn't a template don't use noexcept
with any function pointer types used in the function's type. This and the last point rely on the fact that only non-throwing function pointer types result in naming mangling changes and that non-throwing function pointers can be assigned (C++11) or implicitly converted (C++17) to possibly throwing function pointers.
Upvotes: 21
Reputation: 403
The issue they are warning you about is that in C++14, this will work:
void call(void (*f)())
{
f();
}
void func() noexcept {}
int main(int argc, char* argv[])
{
call(&func);
return 0;
}
but in C++17, you would need to change the declaration of call
to be:
void call(void (*f)() noexcept)
{
f();
}
Since you have defined call
to be a template, you don't need to worry about this. Still, it could cause you problems because the inferred type is changing, which doesn't usually happen.
For example, this code will compile in C++14 but not C++17:
void foo() noexcept {}
void bar() {}
template <typename F>
void call(bool b, F f1, F f2)
{
if (b)
f1();
else
f2();
}
void foobar(bool b)
{
call(b, &foo, &bar);
}
In C++14, the types of foo
and bar
are the same, but they are different in C++17, meaning the template resolution will fail. The error message in gcc 7.2 with the flag -std=c++1z
is:
note: template argument deduction/substitution failed:
note: deduced conflicting types for parameter 'F' ('void (*)() noexcept' and 'void (*)()')
In the example you've given, there is no issue and you won't have a problem compiling in C++14 or C++17 mode. If the code is more complex than the example here (e.g. similar to the examples I gave above), you could encounter some compiler problems. It seems you have a recent compiler; try compiling with -std=c++1z
and see if there are warnings or errors.
Upvotes: 0