Avoid or warn on implicit conversion from const char* to bool in GCC
Consider the following code:
void foo(bool parameter) {
std::cout << parameter << "n";
}
int main() {
foo("const char *argument");
}
I want the compiler to raise a warning when passing const char*
instead of bool
as a parameter to function foo
.
But GCC implicitly converts it. I tried -Wall
, -Wextra
, and -Wpedantic
, but none of these issue a warning. Is there a flag that could catch such an implicit conversion (invalid parameter type)?
Ignore the fact that the function has an argument of type bool
, which some may see as bad code style. I can't refactor that part.
The standard just mentions such an implicit conversion will occur:
A prvalue of integral, floating-point, unscoped enumeration, pointer, and pointer-to-member types can be converted to a prvalue of type bool.
I know such behavior is very convenient in if (ptr)
statements, but for me, in the case of passing parameters, it is clearly wrong and a source of bugs.
c++ gcc implicit-conversion gcc-warning
|
show 2 more comments
Consider the following code:
void foo(bool parameter) {
std::cout << parameter << "n";
}
int main() {
foo("const char *argument");
}
I want the compiler to raise a warning when passing const char*
instead of bool
as a parameter to function foo
.
But GCC implicitly converts it. I tried -Wall
, -Wextra
, and -Wpedantic
, but none of these issue a warning. Is there a flag that could catch such an implicit conversion (invalid parameter type)?
Ignore the fact that the function has an argument of type bool
, which some may see as bad code style. I can't refactor that part.
The standard just mentions such an implicit conversion will occur:
A prvalue of integral, floating-point, unscoped enumeration, pointer, and pointer-to-member types can be converted to a prvalue of type bool.
I know such behavior is very convenient in if (ptr)
statements, but for me, in the case of passing parameters, it is clearly wrong and a source of bugs.
c++ gcc implicit-conversion gcc-warning
7
...and good for bad surprises: If there is a function which acceptsstd::string
and another which acceptsbool
instead, guess which one is chosen for"const char *argument"
. (Spoiler alert: yes the second - and it drove me crazy until I got a clue.) ;-)
– Scheff
15 hours ago
2
a bit offtopic, but I am puzzled why you think having abool
parameter would be bad coding-style..
– user463035818
15 hours ago
1
@user463035818 "Uncle Bob said so" could be a good reason. He argues that function withbool
parameter never has a single responsibility. Uncle Bob has many strong opinions about coding though.
– Yksisarvinen
15 hours ago
@Yksisarvinen you mean that there should be afooTrue
and afooFalse
instead? Hum, maybe i could agree in some cases, but not in general, though if Bob said so there must be some truth to it
– user463035818
15 hours ago
You may have another overloadvoid foo(const char *parameter)
.
– Hi I'm Frogatto
15 hours ago
|
show 2 more comments
Consider the following code:
void foo(bool parameter) {
std::cout << parameter << "n";
}
int main() {
foo("const char *argument");
}
I want the compiler to raise a warning when passing const char*
instead of bool
as a parameter to function foo
.
But GCC implicitly converts it. I tried -Wall
, -Wextra
, and -Wpedantic
, but none of these issue a warning. Is there a flag that could catch such an implicit conversion (invalid parameter type)?
Ignore the fact that the function has an argument of type bool
, which some may see as bad code style. I can't refactor that part.
The standard just mentions such an implicit conversion will occur:
A prvalue of integral, floating-point, unscoped enumeration, pointer, and pointer-to-member types can be converted to a prvalue of type bool.
I know such behavior is very convenient in if (ptr)
statements, but for me, in the case of passing parameters, it is clearly wrong and a source of bugs.
c++ gcc implicit-conversion gcc-warning
Consider the following code:
void foo(bool parameter) {
std::cout << parameter << "n";
}
int main() {
foo("const char *argument");
}
I want the compiler to raise a warning when passing const char*
instead of bool
as a parameter to function foo
.
But GCC implicitly converts it. I tried -Wall
, -Wextra
, and -Wpedantic
, but none of these issue a warning. Is there a flag that could catch such an implicit conversion (invalid parameter type)?
Ignore the fact that the function has an argument of type bool
, which some may see as bad code style. I can't refactor that part.
The standard just mentions such an implicit conversion will occur:
A prvalue of integral, floating-point, unscoped enumeration, pointer, and pointer-to-member types can be converted to a prvalue of type bool.
I know such behavior is very convenient in if (ptr)
statements, but for me, in the case of passing parameters, it is clearly wrong and a source of bugs.
c++ gcc implicit-conversion gcc-warning
c++ gcc implicit-conversion gcc-warning
edited 11 mins ago
Cody Gray♦
192k35377464
192k35377464
asked 15 hours ago
Szymon ZimnowodaSzymon Zimnowoda
1215
1215
7
...and good for bad surprises: If there is a function which acceptsstd::string
and another which acceptsbool
instead, guess which one is chosen for"const char *argument"
. (Spoiler alert: yes the second - and it drove me crazy until I got a clue.) ;-)
– Scheff
15 hours ago
2
a bit offtopic, but I am puzzled why you think having abool
parameter would be bad coding-style..
– user463035818
15 hours ago
1
@user463035818 "Uncle Bob said so" could be a good reason. He argues that function withbool
parameter never has a single responsibility. Uncle Bob has many strong opinions about coding though.
– Yksisarvinen
15 hours ago
@Yksisarvinen you mean that there should be afooTrue
and afooFalse
instead? Hum, maybe i could agree in some cases, but not in general, though if Bob said so there must be some truth to it
– user463035818
15 hours ago
You may have another overloadvoid foo(const char *parameter)
.
– Hi I'm Frogatto
15 hours ago
|
show 2 more comments
7
...and good for bad surprises: If there is a function which acceptsstd::string
and another which acceptsbool
instead, guess which one is chosen for"const char *argument"
. (Spoiler alert: yes the second - and it drove me crazy until I got a clue.) ;-)
– Scheff
15 hours ago
2
a bit offtopic, but I am puzzled why you think having abool
parameter would be bad coding-style..
– user463035818
15 hours ago
1
@user463035818 "Uncle Bob said so" could be a good reason. He argues that function withbool
parameter never has a single responsibility. Uncle Bob has many strong opinions about coding though.
– Yksisarvinen
15 hours ago
@Yksisarvinen you mean that there should be afooTrue
and afooFalse
instead? Hum, maybe i could agree in some cases, but not in general, though if Bob said so there must be some truth to it
– user463035818
15 hours ago
You may have another overloadvoid foo(const char *parameter)
.
– Hi I'm Frogatto
15 hours ago
7
7
...and good for bad surprises: If there is a function which accepts
std::string
and another which accepts bool
instead, guess which one is chosen for "const char *argument"
. (Spoiler alert: yes the second - and it drove me crazy until I got a clue.) ;-)– Scheff
15 hours ago
...and good for bad surprises: If there is a function which accepts
std::string
and another which accepts bool
instead, guess which one is chosen for "const char *argument"
. (Spoiler alert: yes the second - and it drove me crazy until I got a clue.) ;-)– Scheff
15 hours ago
2
2
a bit offtopic, but I am puzzled why you think having a
bool
parameter would be bad coding-style..– user463035818
15 hours ago
a bit offtopic, but I am puzzled why you think having a
bool
parameter would be bad coding-style..– user463035818
15 hours ago
1
1
@user463035818 "Uncle Bob said so" could be a good reason. He argues that function with
bool
parameter never has a single responsibility. Uncle Bob has many strong opinions about coding though.– Yksisarvinen
15 hours ago
@user463035818 "Uncle Bob said so" could be a good reason. He argues that function with
bool
parameter never has a single responsibility. Uncle Bob has many strong opinions about coding though.– Yksisarvinen
15 hours ago
@Yksisarvinen you mean that there should be a
fooTrue
and a fooFalse
instead? Hum, maybe i could agree in some cases, but not in general, though if Bob said so there must be some truth to it– user463035818
15 hours ago
@Yksisarvinen you mean that there should be a
fooTrue
and a fooFalse
instead? Hum, maybe i could agree in some cases, but not in general, though if Bob said so there must be some truth to it– user463035818
15 hours ago
You may have another overload
void foo(const char *parameter)
.– Hi I'm Frogatto
15 hours ago
You may have another overload
void foo(const char *parameter)
.– Hi I'm Frogatto
15 hours ago
|
show 2 more comments
2 Answers
2
active
oldest
votes
You could declare an overload of foo
for pointers as deleted
:
template <class T>
void foo(T*) = delete;
Or better yet, as @Ted comments, simply declare a vanilla overload to not compile any implicit conversions:
template <class T>
void foo(T) = delete;
2
Not wrong but in a real world example these are probably (multiple) functions (maybe even from other files/sources) and that could make this seem quite exhausting.
– Stack Danny
15 hours ago
3
Starting from C++11, you shoul actually declare it asdeleted
… this will generate earlier and more meaningful error messages. Alternatively, declare it with a body that raises astatic_assert
ifT
is notbool
(template is ill-formed if it always raises astatic_assert
, that's the only reason for this check).
– Arne Vogel
15 hours ago
2
... or justvoid foo(T) = delete;
to delete all implicit conversions.
– Ted Lyngmo
14 hours ago
add a comment |
I want the compiler to raise a warning when passing
const char*
instead ofbool
as a parameter to functionfoo
. ... I tried-Wall
,-Wextra
, and-Wpedantic
You need to add -Wconversion
to your compiler flags. Note that seems to work with clang
(recent or older version), but not with gcc
.
If this triggers too many warnings that you don't want to handle, you can selectively enable -Wstring-conversion
(clang
only).
1
I tried but without luck: Live Demo on coliru. Did I oversee something?
– Scheff
15 hours ago
1
@Scheff Weird, it seems to be handled differently inclang
andgcc
. Updated the answer accordingly.
– lubgr
15 hours ago
1
I just found this The new Wconversion option and in fact the implicit conversion fromconst char*
tobool
isn't mentioned at all. Looks like it isn't covered in gcc...
– Scheff
15 hours ago
@Scheff fwiw also no error/warning for uniform initialization of a bool with aconst char*
– user463035818
15 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54515792%2favoid-or-warn-on-implicit-conversion-from-const-char-to-bool-in-gcc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You could declare an overload of foo
for pointers as deleted
:
template <class T>
void foo(T*) = delete;
Or better yet, as @Ted comments, simply declare a vanilla overload to not compile any implicit conversions:
template <class T>
void foo(T) = delete;
2
Not wrong but in a real world example these are probably (multiple) functions (maybe even from other files/sources) and that could make this seem quite exhausting.
– Stack Danny
15 hours ago
3
Starting from C++11, you shoul actually declare it asdeleted
… this will generate earlier and more meaningful error messages. Alternatively, declare it with a body that raises astatic_assert
ifT
is notbool
(template is ill-formed if it always raises astatic_assert
, that's the only reason for this check).
– Arne Vogel
15 hours ago
2
... or justvoid foo(T) = delete;
to delete all implicit conversions.
– Ted Lyngmo
14 hours ago
add a comment |
You could declare an overload of foo
for pointers as deleted
:
template <class T>
void foo(T*) = delete;
Or better yet, as @Ted comments, simply declare a vanilla overload to not compile any implicit conversions:
template <class T>
void foo(T) = delete;
2
Not wrong but in a real world example these are probably (multiple) functions (maybe even from other files/sources) and that could make this seem quite exhausting.
– Stack Danny
15 hours ago
3
Starting from C++11, you shoul actually declare it asdeleted
… this will generate earlier and more meaningful error messages. Alternatively, declare it with a body that raises astatic_assert
ifT
is notbool
(template is ill-formed if it always raises astatic_assert
, that's the only reason for this check).
– Arne Vogel
15 hours ago
2
... or justvoid foo(T) = delete;
to delete all implicit conversions.
– Ted Lyngmo
14 hours ago
add a comment |
You could declare an overload of foo
for pointers as deleted
:
template <class T>
void foo(T*) = delete;
Or better yet, as @Ted comments, simply declare a vanilla overload to not compile any implicit conversions:
template <class T>
void foo(T) = delete;
You could declare an overload of foo
for pointers as deleted
:
template <class T>
void foo(T*) = delete;
Or better yet, as @Ted comments, simply declare a vanilla overload to not compile any implicit conversions:
template <class T>
void foo(T) = delete;
edited 13 hours ago
Ted Lyngmo
2,5211317
2,5211317
answered 15 hours ago
Paul EvansPaul Evans
19k32038
19k32038
2
Not wrong but in a real world example these are probably (multiple) functions (maybe even from other files/sources) and that could make this seem quite exhausting.
– Stack Danny
15 hours ago
3
Starting from C++11, you shoul actually declare it asdeleted
… this will generate earlier and more meaningful error messages. Alternatively, declare it with a body that raises astatic_assert
ifT
is notbool
(template is ill-formed if it always raises astatic_assert
, that's the only reason for this check).
– Arne Vogel
15 hours ago
2
... or justvoid foo(T) = delete;
to delete all implicit conversions.
– Ted Lyngmo
14 hours ago
add a comment |
2
Not wrong but in a real world example these are probably (multiple) functions (maybe even from other files/sources) and that could make this seem quite exhausting.
– Stack Danny
15 hours ago
3
Starting from C++11, you shoul actually declare it asdeleted
… this will generate earlier and more meaningful error messages. Alternatively, declare it with a body that raises astatic_assert
ifT
is notbool
(template is ill-formed if it always raises astatic_assert
, that's the only reason for this check).
– Arne Vogel
15 hours ago
2
... or justvoid foo(T) = delete;
to delete all implicit conversions.
– Ted Lyngmo
14 hours ago
2
2
Not wrong but in a real world example these are probably (multiple) functions (maybe even from other files/sources) and that could make this seem quite exhausting.
– Stack Danny
15 hours ago
Not wrong but in a real world example these are probably (multiple) functions (maybe even from other files/sources) and that could make this seem quite exhausting.
– Stack Danny
15 hours ago
3
3
Starting from C++11, you shoul actually declare it as
deleted
… this will generate earlier and more meaningful error messages. Alternatively, declare it with a body that raises a static_assert
if T
is not bool
(template is ill-formed if it always raises a static_assert
, that's the only reason for this check).– Arne Vogel
15 hours ago
Starting from C++11, you shoul actually declare it as
deleted
… this will generate earlier and more meaningful error messages. Alternatively, declare it with a body that raises a static_assert
if T
is not bool
(template is ill-formed if it always raises a static_assert
, that's the only reason for this check).– Arne Vogel
15 hours ago
2
2
... or just
void foo(T) = delete;
to delete all implicit conversions.– Ted Lyngmo
14 hours ago
... or just
void foo(T) = delete;
to delete all implicit conversions.– Ted Lyngmo
14 hours ago
add a comment |
I want the compiler to raise a warning when passing
const char*
instead ofbool
as a parameter to functionfoo
. ... I tried-Wall
,-Wextra
, and-Wpedantic
You need to add -Wconversion
to your compiler flags. Note that seems to work with clang
(recent or older version), but not with gcc
.
If this triggers too many warnings that you don't want to handle, you can selectively enable -Wstring-conversion
(clang
only).
1
I tried but without luck: Live Demo on coliru. Did I oversee something?
– Scheff
15 hours ago
1
@Scheff Weird, it seems to be handled differently inclang
andgcc
. Updated the answer accordingly.
– lubgr
15 hours ago
1
I just found this The new Wconversion option and in fact the implicit conversion fromconst char*
tobool
isn't mentioned at all. Looks like it isn't covered in gcc...
– Scheff
15 hours ago
@Scheff fwiw also no error/warning for uniform initialization of a bool with aconst char*
– user463035818
15 hours ago
add a comment |
I want the compiler to raise a warning when passing
const char*
instead ofbool
as a parameter to functionfoo
. ... I tried-Wall
,-Wextra
, and-Wpedantic
You need to add -Wconversion
to your compiler flags. Note that seems to work with clang
(recent or older version), but not with gcc
.
If this triggers too many warnings that you don't want to handle, you can selectively enable -Wstring-conversion
(clang
only).
1
I tried but without luck: Live Demo on coliru. Did I oversee something?
– Scheff
15 hours ago
1
@Scheff Weird, it seems to be handled differently inclang
andgcc
. Updated the answer accordingly.
– lubgr
15 hours ago
1
I just found this The new Wconversion option and in fact the implicit conversion fromconst char*
tobool
isn't mentioned at all. Looks like it isn't covered in gcc...
– Scheff
15 hours ago
@Scheff fwiw also no error/warning for uniform initialization of a bool with aconst char*
– user463035818
15 hours ago
add a comment |
I want the compiler to raise a warning when passing
const char*
instead ofbool
as a parameter to functionfoo
. ... I tried-Wall
,-Wextra
, and-Wpedantic
You need to add -Wconversion
to your compiler flags. Note that seems to work with clang
(recent or older version), but not with gcc
.
If this triggers too many warnings that you don't want to handle, you can selectively enable -Wstring-conversion
(clang
only).
I want the compiler to raise a warning when passing
const char*
instead ofbool
as a parameter to functionfoo
. ... I tried-Wall
,-Wextra
, and-Wpedantic
You need to add -Wconversion
to your compiler flags. Note that seems to work with clang
(recent or older version), but not with gcc
.
If this triggers too many warnings that you don't want to handle, you can selectively enable -Wstring-conversion
(clang
only).
edited 13 hours ago
Peter Mortensen
13.6k1984111
13.6k1984111
answered 15 hours ago
lubgrlubgr
11.5k21846
11.5k21846
1
I tried but without luck: Live Demo on coliru. Did I oversee something?
– Scheff
15 hours ago
1
@Scheff Weird, it seems to be handled differently inclang
andgcc
. Updated the answer accordingly.
– lubgr
15 hours ago
1
I just found this The new Wconversion option and in fact the implicit conversion fromconst char*
tobool
isn't mentioned at all. Looks like it isn't covered in gcc...
– Scheff
15 hours ago
@Scheff fwiw also no error/warning for uniform initialization of a bool with aconst char*
– user463035818
15 hours ago
add a comment |
1
I tried but without luck: Live Demo on coliru. Did I oversee something?
– Scheff
15 hours ago
1
@Scheff Weird, it seems to be handled differently inclang
andgcc
. Updated the answer accordingly.
– lubgr
15 hours ago
1
I just found this The new Wconversion option and in fact the implicit conversion fromconst char*
tobool
isn't mentioned at all. Looks like it isn't covered in gcc...
– Scheff
15 hours ago
@Scheff fwiw also no error/warning for uniform initialization of a bool with aconst char*
– user463035818
15 hours ago
1
1
I tried but without luck: Live Demo on coliru. Did I oversee something?
– Scheff
15 hours ago
I tried but without luck: Live Demo on coliru. Did I oversee something?
– Scheff
15 hours ago
1
1
@Scheff Weird, it seems to be handled differently in
clang
and gcc
. Updated the answer accordingly.– lubgr
15 hours ago
@Scheff Weird, it seems to be handled differently in
clang
and gcc
. Updated the answer accordingly.– lubgr
15 hours ago
1
1
I just found this The new Wconversion option and in fact the implicit conversion from
const char*
to bool
isn't mentioned at all. Looks like it isn't covered in gcc...– Scheff
15 hours ago
I just found this The new Wconversion option and in fact the implicit conversion from
const char*
to bool
isn't mentioned at all. Looks like it isn't covered in gcc...– Scheff
15 hours ago
@Scheff fwiw also no error/warning for uniform initialization of a bool with a
const char*
– user463035818
15 hours ago
@Scheff fwiw also no error/warning for uniform initialization of a bool with a
const char*
– user463035818
15 hours ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54515792%2favoid-or-warn-on-implicit-conversion-from-const-char-to-bool-in-gcc%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
7
...and good for bad surprises: If there is a function which accepts
std::string
and another which acceptsbool
instead, guess which one is chosen for"const char *argument"
. (Spoiler alert: yes the second - and it drove me crazy until I got a clue.) ;-)– Scheff
15 hours ago
2
a bit offtopic, but I am puzzled why you think having a
bool
parameter would be bad coding-style..– user463035818
15 hours ago
1
@user463035818 "Uncle Bob said so" could be a good reason. He argues that function with
bool
parameter never has a single responsibility. Uncle Bob has many strong opinions about coding though.– Yksisarvinen
15 hours ago
@Yksisarvinen you mean that there should be a
fooTrue
and afooFalse
instead? Hum, maybe i could agree in some cases, but not in general, though if Bob said so there must be some truth to it– user463035818
15 hours ago
You may have another overload
void foo(const char *parameter)
.– Hi I'm Frogatto
15 hours ago