Following code compiles fine without any warnings (with default options to g++). Is there a flag that we can use to ask g++ to emit warnings in such cases?
void foo(bool v) {
}
void bar() {
foo("test");
}
Following code compiles fine without any warnings (with default options to g++). Is there a flag that we can use to ask g++ to emit warnings in such cases?
void foo(bool v) {
}
void bar() {
foo("test");
}
I like to try
clang -Weverything
and pick the warnings that pop out:Unfortunately, neither
-Wstring-conversion
nor-Wnull-conversion
is supported by g++. You could try and submit feature request / bug report to gcc.If I really wanted to prevent such a function being passed a pointer, I would do this in C++11;
which will trigger a compiler error.
Before C++11 (not everyone can update for various reasons) a technique is;
and then have the definition of
foo(bool)
somewhere (in one and only one compilation unit within your build). For most toolchains that use a traditional compiler and linker (which is most toolchains in practice, including most installations of g++) the linker error is caused byfoo<char const>(char const*)
not being defined. The precise wording of the error is linker dependent.Note that the errors can be deliberately circumvented by a developer. But such techniques will stop accidental usage.
If you want to allow any pointer except a
char const *
being passed, simply declarevoid foo(const char *)
as above and don't declare the template.