Support explicit parameter functions#107121
Support explicit parameter functions#107121lawnjelly wants to merge 1 commit intogodotengine:masterfrom
Conversation
Prevent implicit parameter conversions for specific functions using a macro.
|
I tinkered a bit, looks like with C++20 it will be possible in a pretty minimalistic way: template <typename T, typename U>
concept exact_type_t = std::is_same_v<T, U>;
int add(exact_type_t<int> auto p_lhs, exact_type_t<int> auto p_rhs) {
return p_lhs + p_rhs;
}
int main() {
std::cout << add(2, 3); // Works
std::cout << add(2l, 3); // Static error
return 0;
}It's especially nice how no Edit: Actually, this exact concept is a core C++20 feature apparently, so we don't even need our own concept: int add(std::same_as<int> auto p_lhs, std::same_as<int> auto p_rhs) {
return p_lhs + p_rhs;
}
int main() {
std::cout << add(2, 3); // Works
std::cout << add(2l, 3); // Static error
return 0;
} |
|
Sorry, was away, that sounds great that there is a more idiomatic way of doing this in c++20! The whole It would have been nice to have something that could be used in 3.x too but I guess the codebase is pretty mature and it isn't so much of a problem (even error checking could be done by temporarily adding the compiler warning I guess). I'll close this then, but we can reference it when we discuss c++20 changes in a core meeting for 4.6. 👍 |
Prevent implicit parameter conversions for specific functions using a macro.
Implicit conversion can sometimes be an indicator of bugs, we use them quite a bit with the basic types, but we identified a desire in the core meeting today to be able to selectively apply explicit protection for parameters in certain functions.
Just adding a draft here to foster more discussion, I'm sure template / macro fu could be applied to make it "more better". 😁
Notes
deletingthese versions.explicit_param<float>) as I couldn't get it to acceptfloatinput.