Pybind11: How to assign default value for a struct member variable? - c++

I am trying to create python bindings for the below
struct example {
int a = 1;
int b = 2;
};
This is what I have so far
PYBIND11_MODULE(example_python, m) {
py::class_<example>(m, "example")
.def_readwrite("a", &example::a)
.def_readwrite("b", &example::b);
}
when I checked in python, both a and b are empty, so I tried something like
PYBIND11_MODULE(example_python, m) {
py::class_<example>(m, "example")
.def_readwrite("a", &example::a, py::arg("a") = 1)
.def_readwrite("b", &example::b, py::arg("b") = 2);
}
but this results in a compilation error. I went through the documentation multiple times but couldn't a way to do it. Can someone let me know how to assign the default values in pybind11?

Your code doesn't compile, at least on my machine, because you didn't bind any constructor. However, if you do so then the default values become populated (because that's part of what a constructor does!). In other words, just use this:
PYBIND11_MODULE(example_python, m) {
py::class_<example>(m, "example")
.def(py::init<>()) // <-- bind the default constructor
.def_readwrite("a", &example::a)
.def_readwrite("b", &example::b);
}

Related

Is there a less verbose idiom for unpacking an optional in C++?

In the project I am currently working on I find myself writing a lot of code that looks like the following, where, get_optional_foo is returning an std::optional:
//...
auto maybe_foo = get_optional_foo(quux, ...)
if (!maybe_foo.has_value())
return {};
auto foo = maybe_foo.value()
//...
// continue on, doing things with foo...
I want to bail out of the function if I get a null option; otherwise, I want to assign a non-optional variable to the value. I've started using the convention of naming the optional with a maybe_ prefix but am wondering if there is some way of doing this such that I don't need to use a temporary for the optional at all? This variable is only ever going to be used to check for a null option and dereference if there is a value.
You don't need an intermediate object. std::optional supports a pointer interface to access it so you can just use it like:
//...
auto foo = get_optional_foo(quux, ...)
if (!foo)
return {};
//...
foo->some_member;
(*foo).some_member;
Slightly different than what you are asking, but consider:
if (auto foo = get_optional_foo(1)) {
// ...
return foo->x;
} else {
return {};
}
This places the main body of the function in an if() block, which may be more readable.
Shortest I can think of:
auto maybe_foo = get_optional_foo(quux, ...)
if (!maybe_foo) return {};
auto &foo = *maybe_foo; // alternatively, use `*maybe_foo` below
If you have multiple optionals in the function and it's very unlikely they'll be empty you can wrap the whole thing with a try - catch.
try {
auto &foo = get_optional_foo(quux, ...).value();
auto &bar = get_optional_bar(...).value();
...
} catch (std::bad_optional_access &e) {
return {};
}

How do I declare an array of red black trees?

When I want to initialize a red black tree I do as in the documentation.
auto rbt = redBlackTree(1,2,3,4)
but if I want to declare it globally or make an array of red black trees I don't know how to do it and the documentation is not helping. I've tried various things and I frequently get errors similar to: redBlackTree!int is used as a type Can you help me? I could do it if I knew what to put instead of auto, ie, if I knew the type of redBlackTree.
I want to declare a red black tree in global scope or declare an array for which I need to declare the type, I want to do something like this:
type rbt;
void main() {
rbt.insert(3);
}
or this:
void main{
type[2] rbt;
rbt[0].insert(1);
}
You don't need to know the type of redBlackTree. You can query for at compile-time with typeof:
alias RBTree = typeof(redBlackTree(1));
RBTree rbt = redBlackTree(1, 2, 3);
This is a common and an encouraged pattern as many functions in D return Voldemort types (types that cannot be named).
In your example the type is RedBlackTree!int. If you don't use an IDE, an easy way to discover the type is pragma(msg, typeof(<functionCall>(<args>)));.
Furthermore, I should note that declaring an array of RedBlackTree works with auto:
auto arr = [redBlackTree(1, 2), redBlackTree(3, 4)];
For more help, please feel free to post the exact code that failed.
The type (using long instead of int) is RedBlackTree!long, here are some examples. Remember you have to use new to initialize the class.
import std.stdio;
import std.container;
RedBlackTree!long rbtree;
RedBlackTree!long[2] rbarray;
RedBlackTree!long[] rbdynamicarr;
RedBlackTree!long[][] rbmat;
void main() {
rbtree.writeln;
rbtree = new RedBlackTree!long;
rbtree.insert(3);
rbtree.writeln;
rbarray.writeln;
rbarray = new RedBlackTree!long[2];
rbarray.writeln;
rbdynamicarr.writeln;
int n = 3;
rbdynamicarr = new RedBlackTree!long[n];
rbdynamicarr.writeln;
rbmat.writeln;
int m = 2;
rbmat = new RedBlackTree!long[][](n,m);
rbmat.writeln;
alias RBTree = typeof(redBlackTree!long(1L));
RBTree rbalias;
rbalias = new RBTree;
rbalias.writeln;
RBTree[3] crayola;
crayola.writeln;
typeid(redBlackTree(1)).writeln;
RedBlackTree!(long, "a < b", false) hola;
hola = new RedBlackTree!(long, "a < b", false);
hola.writeln;
}

How to know if a parameter wasn't passed in when creating an object?

I'm translating some code, and in this completely unused private language, the code states that if more than 4 parameters were passed in (as the 5th parameter is optional), then do something to the value of that parameter, like so:
if (ParamCount > 4) {
if (ID == 0) {
ID = 0x700;
if (mtrx > 0) {ID = 0x11AA0FF0; }
}
BaseID = ID;
} else {
BaseID = ID;
}
How would I write this out in C++98? I created a constructor which takes in those parameters, as I am trying to turn this bit of code into a class, yet when a parameter is optional you have to set it a value, like 0, or so I thought.
Overload the function instead.
The version with 5 parameters uses the true part of the if.
The other version uses the false part of the if.

How to copy an object if (and only if) it has a copy constructor?

Context: I'm trying to memoize an object of a template class. Right now, the class is a deeply nested data structure full of unique pointers, and so doesn't have a copy constructor (and so would be impossible to cache, as far as I know). However, in the future, I would like to allow memoization if a copy constructor is available. I tried the following code:
// some function here... {
static std::unordered_map<State, Result> cache;
return [c, ToValue](State state) {
if (cache.find(state) != cache.end()) {
std::cout << "retrieving Literal from cache\n";
if (std::is_copy_constructible<Result>::value) {
return cache[state];
}
}
// calculate and return a Result
This code doesn't compile because Result doesn't have a copy constructor. Is there any way to get around this? Google is being quite unhelpful.
I'm presuming the error you are getting is that return cache[state]; cannot be compiled when the object is not copy-constructible. To fix that you can write:
if constexpr (std::is_copy_constructible<Result>::value) {
return cache[state];
}
If you are still having trouble then post a MCVE that has the error.
As others have commented, the question is rather ill-defined and a bit confused, but do you need to actually copy an object in order to cache it?
Actually, no. You can use std::shared_ptr to share ownership of the object between the creator, any consumers, and the cache. If nothing else, this is much more efficient if your object is a complex one. It will also work for any type of object, copyable or not.
Example (I'm going to use the word Key rather than State, for what I hope are obvious reasons).
Given these declarations:
class MyKey
{
// ....
};
class MyCacheableObject
{
// Constructor
MyCacheableObject (int a, int b, int c) { ... }
// ...
};
static std::unordered_map<MyKey, std::shared_ptr<MyCacheableObject>> cache; // or std::map
You can do this (please note that there are other ways to make a std::shared_ptr, see here):
std::shared_ptr<MyCacheableObject> CreateCacheableObject (int a, int b, int c)
{
return std::make_shared<MyCacheableObject> (MyCacheableObject (a, b, c));
}
And then, assuming you have a key you plan to use to retrieve the object from the cache later on, you can do:
MyKey someKey = ...;
std::shared_ptr<MyCacheableObject> newObject = CreateCacheableObject (1, 2, 3);
// ... setup / use `newObject` in whatever way is appropriate to your use-case
cache [someKey] = newObject;
And you can of course retrieve the object from the cache (if it's in there) via:
auto retrievedObject = cache.find (someKey)
if (retrievedObject != cache.end())
...
So this question is not about whether an object is copyable at all. It's about (shared) ownership and std::shared_ptr takes care of all that for you, you don't really have to think about it. Oy vay.
There's a live demo, to show that this all compiles, here.

Defining const "variable" inside if block

I have the following code:
Foo a;
if (some_fairly_long_condition) {
a = complicated_expression_to_make_foo_1();
} else {
a = complicated_expression_to_make_foo_2();
}
I have two issues with this:
a is a const and should be declared so
the "empty" constructor, Foo() is called for no reason (maybe this is optimised away?)
One way to fix it is by using the ternary operator:
const Foo a = some_fairly_long_condition?
complicated_expression_to_make_foo_1():
complicated_expression_to_make_foo_2();
Is this good practice? How do you go about it?
To answer the second part of your question:
I usually put the initialization code into a lambda:
const Foo a = [&]()->Foo{
if (some_fairly_long_condition) {
return complicated_expression_to_make_foo_1();
} else {
return complicated_expression_to_make_foo_2();
}
}();
In most cases you should even be able to omit the trailing return type, so you can write
const Foo a = [&](){ ...
As far as the first part is concerned:
I'd say that greatly depends on how complex your initialization code is. If all three parts are really complicated expressions (and not just a function call each) then the solution with the ternary operator becomes an unreadable mess, while the lambda method (or a separate named function for that matter) allows you to break up those parts into the respective sub expressions.
If the problem is to avoid ternaty operator and your goal is to define the constant a, this code is an option:
Foo aux;
if (some_fairly_long_condition) {
aux = complicated_expression_to_make_foo_1();
} else {
aux = complicated_expression_to_make_foo_2();
}
const Foo a(aux);
It is a good solution, without any new feature ---as lambdas--- and including the code inline, as you want.