Proposal for explict keyword for non constructor functions

Hi all,

The previous topic was archived.

So I did get a reply from Jason Turner, he would like to see something like this, just that he had no idea about how much traction it would get. He also said he didn't think there would be a lot of compiler magic, justt not take into account conversions when looking up matching functions.

Then I found something similar to this in one of Jason's videos "Back to Basics, API Design"

https://godbolt.org/z/K8jfoqjE1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
double foo(double a, double b) {
    return a * b;
}

// c++20 implicit template function
// compilation matches this function with non explicit arguments
// function #1
auto foo(const auto&, const auto&) = delete;

int main() {
   double a = foo(2.0, 3.0);

    //compilation fails here, floats used not doubles
   double b = foo(3.0f, 4.0f); 
}


So this looks like just the thing, have this:

1
2
3
4
// proposed use of the explicit keyword 
explicit double foo(double a, double b) {
    return a * b;
}


implicitly converted into this:

auto foo(const auto&, const auto&) = delete;

That seems pretty simple to deal with, and because it is already c++ code, cpp2 can implement it easily too.

Maybe I should go for another big gun, and send an email to Herb!!

It is trickier to allow explicit for some of the parameters, along with possible promotion of the others:

[/code]
// proposed use of the explicit keyword
// we don't care if a short is promoted to int, for example
// but we really do want 1st argument to be double
double foo(explicit double a, int b) {
return a * b;
}
auto foo(const auto&, const int) = delete;
[/code]

but this fails because it matches function #1 above, non explicit arguments:
1
2
unsigned short d = 2;
   double c = foo(5.0, d); // d is not int 


Well, I would be happy if all the arguments had to be explicit :+) , never mind about some of them.
Registered users can post here. Sign in or register to post.