Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

One Constructor with default values instead of multiple ctors with different number of arguments #68

Closed
yosoufe opened this issue Apr 17, 2019 · 2 comments

Comments

@yosoufe
Copy link
Contributor

yosoufe commented Apr 17, 2019

Hi. I am just wondering have you tried creating one constructor with default arguments the same way as it is described in pybind11 documentations??

I tried to do that. I did not find a correct way that clang stuff may support for default value of the argument. So I did it in this way:
https://github.com/RosettaCommons/binder/compare/master...yosoufe:defaultValueForDefaultArguments?expand=1
I have already seen some problem with it that it does not generalize. for example if the argument type is Enum, in the C++ code the Enum should be defined with its all namespaces until it will be compilable.

For example:

struct Astruct{
  enum enumType{
    T1,
    T2
  }
  enumType t;
  double value1;
  Astruct(double v1, enumType type = T1);
}

is compilable. But in my branch, I have to write it in the following way until the bindings are also compilable Astruct(double v1, enumType type = Astruct::enumType::T1); because I simply copy what is after the = into the binding codes.

Do you have any better idea??
And also any other problems that may occur that I cannot see it?

@lyskov
Copy link
Member

lyskov commented Apr 19, 2019

Re using Pybind11 default: - making bindings default arguments that way will be evaluate using Python evaluation strategy (ie evaluate once during binding time) which will be error prone and quite confusing for any non constant defaults (consider for example default that is a function call - in C++ such function would be called each time function is called)

re need to fully qualify names for default parameters: i think that is unavoidable. The core issue here is that default value specified during class declaration and during binding code evaluate in different translation units and there is no general way to map one to the other (consider for example that besides using unqualified declaration from class inner namespace C++ code could also use unqualified default value from unrelated namespaces pulled-in with using detectives). So using lambdas and explicitly generating multiple overloads seems to be the only general solution to me.

@yosoufe
Copy link
Contributor Author

yosoufe commented Apr 19, 2019

That makes sense.

@yosoufe yosoufe closed this as completed Apr 19, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants