How do I expand a tuple into variadic template function’s arguments?

In C++17 you can do this: std::apply(the_function, the_tuple); This already works in Clang++ 3.9, using std::experimental::apply. Responding to the comment saying that this won’t work if the_function is templated, the following is a work-around: #include <tuple> template <typename T, typename U> void my_func(T &&t, U &&u) {} int main(int argc, char *argv[argc]) { std::tuple<int, float> … Read more

Normal arguments vs. keyword arguments

There are two related concepts, both called “keyword arguments“. On the calling side, which is what other commenters have mentioned, you have the ability to specify some function arguments by name. You have to mention them after all of the arguments without names (positional arguments), and there must be default values for any parameters which … Read more

TypeError: method() takes 1 positional argument but 2 were given

In Python, this: my_object.method(“foo”) …is syntactic sugar, which the interpreter translates behind the scenes into: MyClass.method(my_object, “foo”) …which, as you can see, does indeed have two arguments – it’s just that the first one is implicit, from the point of view of the caller. This is because most methods do some work with the object … Read more

Set a default parameter value for a JavaScript function

From ES6/ES2015, default parameters are in the language specification. function read_file(file, delete_after = false) { // Code } just works. Reference: Default Parameters – MDN Default function parameters allow formal parameters to be initialized with default values if no value or undefined is passed. You can also simulate default named parameters via destructuring: // the … Read more