Replies: 4 comments 10 replies
-
If you want to disable implicit cast you can use concepts (even in cpp1) - not perfect but works. void format(std::same_as<int> auto count, std::same_as<char> auto padding); // no more implicit casts |
Beta Was this translation helpful? Give feedback.
-
Thanks for the suggestion! Ah, Cpp1 implicit narrowing fun 😉 Cppfront doesn't do function call name lookup and overload resolution, so we don't have the information even for all-Cpp2 function calls. We leave that to the Cpp1 compiler. However, Cpp2 does already help where it can, in particular that all initialization is lowered to Cpp1 struct X {
X(char) { }
};
main: () = {
x1: X = 'a'; // ok
x2: X = 420; // Cpp1 error - lowers to 'X x2{420};' which disallows narrowing
} I'm not sure we can reasonably do more than that right now though. |
Beta Was this translation helpful? Give feedback.
-
@hsutter, I did a small investigation into what is possible. The working prototype is here: https://godbolt.org/z/aed3j6nve TLDR: we can achieve the following code void fun(int value, char padding);
void gen(auto value, auto padding);
CALL(fun, 123, ' '); // works
// CALL(fun, 345, 0); // this does not compile - CORRECT!
CALL(gen, 567, 0); // works
// CALL(fun, 'a', ' '); // this does not compile - the current solution requires an exact match for the types (even if there is a valid conversion from char to int) If you are interested in such a solution, I can spend more time to make it work also for arguments that can be safely cast. |
Beta Was this translation helpful? Give feedback.
-
Thanks for looking into this. I appreciate the suggestion but I worry it's adding too much to the code gen (and another inline function call) for probably not enough value? And that it requires an exact match, because I suspect that may get noisy (implicit conversions are often undesirable, but they're also often desirable). If we did it, and it worked well enough to pass regression when enabled (i.e., the exact-match requirement isn't too noisy), then I'd probably want it to be off by default and add an opt-in switch. But most of all I think I'd prefer to look at it later (sorry!). |
Beta Was this translation helpful? Give feedback.
-
I saw a bug report on an open source repo (names changed to protect the innocent) where someone thought that a function took two
int
s when it actually took anint
and achar
. The function used the char for padding, and they were surprised when they ended up with null characters in their output. They figured it out pretty quickly on their own, but I wondered if cpp2 could warn about this, or if there would be too much noise.Could this be a warning like "Warning: passing an integer literal to a function that takes a character." I'm thinking this is similar to a warning about passing an integer literal to a function that takes a pointer. Is there an existing cpp1 compiler warning for this? A quick search didn't find one.
We DO get a cpp1 compiler warning for this:
Beta Was this translation helpful? Give feedback.
All reactions