Variadic Template And Ellipses - variadic-templates

I have two function defined as follows
void Function( ... );
template < typename ... ARGS >
void Function1( ARGS ... args )
{
// I have to call Function here
}
How can i pass variadic template arguments "args" to Function.

You should be able to just call Function(args...).
The ... operator in C++11 expands template parameter packs which is exactly what you want here. See here for more details.

Related

How to get powershell function source with full header

As seen here https://superuser.com/questions/414036/get-definition-of-function-and-echo-the-code this would get the body of test-function
${function:test-function}
but not the header of test-function with parameters
You'll have to add the missing parts yourself (function <name> { ... }) in order to form a full function definition:
$funcDef = "function test-function { ${function:test-function} }"
If you're given the function name in a variable or you want to avoid repeating the function name:
$funcName = 'test-function'
$funcDef = "function $funcName { $(Get-Content function:$funcName) }"
Note that the function body returned does include parameters, in the form of a param(...) block - even if the original definition used the in-line parameter-declaration form. E.g., both function test-function($foo) { 'foo' } and function test-function { param($foo) 'foo' } result in body param($foo) 'foo' (with variations in whitespace).
Background:
${function:test-function} is an instance of namespace variable notation, and therefore only works with literal names.
It returns the body of the specified function as a script block, which when stringified, results in the block's verbatim source code (without the { and } that you use to create a script-block literal.
Get-Content function:test-function is what the namespace variable notation translates into, except that the cmdlet-based enables use of variables in its arguments.

how to use template "inheritance" to avoid boilerplate code when binding C++ to python with pybind11

I managed to bind template definitions doing
py::class_<T<a>>(m, "Ta")
py::class_<T<b>>(m, "Tb")
...
where T is the template class and a and b typenames defining the template. But the problem I have tells me that this is not the really good way to expose these template definitions, because now I am forced to duplicate code for all the functions and parameters of the template class T:
py::class_<T<a>>(m, "Ta")
.def(py::init<string, string>())
.def("do_smthg", &T<a>::do_sthg)
...;
py::class_<T<b>>(m, "Tb")
.def(py::init<string, string>())
.def("do_smthg", &T<b>::do_sthg)
...;
when class T itself for instance has do_smthg, that I would then expect to have to define only once. I tried to browse the doc and the web, but I failed to find the right pointer.
You may just create a simple helper function that instantiates a list of templates, like following:
template <typename x, typename ... Tail, typename ModT>
void ApplyTemplate(ModT & m, std::vector<std::string> names) {
py::class_<T<x>>(m, names.at(0))
.def(py::init<string, string>())
.def("do_smthg", &T<x>::do_sthg)
;
names.erase(names.begin());
if constexpr(sizeof...(Tail) > 0)
ApplyTemplate<Tail...>(m, names);
}
Now call this function within module context with all possible arguments to template:
PYBIND11_MODULE(example, m) {
ApplyTemplate<a, b>(m, {"Ta", "Tb"});
}
If for some reason your T template is not available within ApplyTemplate context then you may pass it as a template <typename> typename argument:
template <template <typename> typename T, typename x, typename ... Tail, typename ModT>
void ApplyTemplate(.....
Instead of function you can also use a simple macro:
PYBIND11_MODULE(example, m) {
// Define macro
#define MY_CLASS(param, name) \
py::class_<T<param>>(m, name) \
.def(py::init<string, string>()) \
.def("do_smthg", &T<param>::do_sthg) \
;
// Use macro
MY_CLASS(a, "Ta");
MY_CLASS(b, "Tb");
// Remove macro
#undef MY_CLASS
}

How to put Doc comments for Dart function parameters?

We can easily put Doc comments for Dart Class variables e.g.
class SomeClass {
/// Class variable Doc Comment.
var someVariable;
}
How can I do the same for Dart Function parameters e.g. I tried this
void someFunction(
{/// Function parameter documentation
String funParameter="Some Default Value"}
) {
}
But it's not showing anything. If it's not possible please suggest me any alternative.
It is against the Effective Dart conventions to document parameters of functions using a direct syntax like that. Instead, use prose to describe the parameter and how it relates to the function's purpose.
// Instead of this
/// someFunction
/// #funParameter Does something fun
void someFunction({ String funParameter="Some Default Value" }) ...
// Or this
/// someFunction
void someFunction({
/// Does something fun
String funParameter="Some Default Value"
}) ...
// Do this
/// Does something fun with the [funParameter].
void someFunction({ String funParameter="Some Default Value" }) ...
Here's perhaps a more practical example:
/// Takes the values [a] and [b] and returns their sum. Optionally a
/// third parameter [c] can be provided and it will be added to the
/// sum as well.
int add(int a, int b, [int c = 0]) ...
You should use the doc comment like this:
/// the function uses [funParameter] to do stuff
void someFunction({String funParameter = "Some Default Value"}) {
// ..
}

what does ":" mean in this case in coffee script?

I am new to coffee script. When I am looking at this document https://atom.io/docs/api/v0.198.0/CommandRegistry#instance-add
I see a code segment like,
atom.commands.add 'atom-text-editor',
'user:insert-date': (event) ->
editor = #getModel()
editor.insertText(new Date().toLocaleString())
while the function signature looks,
::add(target, commandName, callback)
So in the code segment, what does : on the second line mean? My understanding is the 'user:insert-date' before : is commandName in the signature. The thing after : is "callback". So : is a argument separator like a ,? I don't find this introduced in the coffee script document http://coffeescript.org
That colon is just part of an object literal. The braces around object literals are optional in CoffeeScript when there's no ambiguity. If we add the optional braces, we get something that looks more like JavaScript:
atom.commands.add 'atom-text-editor', {
'user:insert-date': (event) ->
#...
}
So atom.commands.add is being called with two arguments. The first is the string 'atom-text-editor' and the second is an object with one key ('user:insert-date') whose value is an anonymous function that takes a single argument.
Appending mu is too short's answer (the user is absolutely correct that the second parameter commandNamecan be an object without the explicit braces {})
Atom's sourcecode:
https://github.com/atom/atom/blob/v0.198.0/src/command-registry.coffee#L81
add: (target, commandName, callback) ->
if typeof commandName is 'object'
commands = commandName
disposable = new CompositeDisposable
for commandName, callback of commands
disposable.add #add(target, commandName, callback)
return disposable

Callback from python

After reading a lot from this wonderful web, I need some help.
I have this function (in a dll):
kvStatus kvSetNotifyCallback ( const int hnd, kvCallback_t callback, void * context,
unsigned int notifyFlags);
How to call this function from Python?
I tried:
kvSetNotifyCallback(c_int(hnd1),c_void_p(self.can_rx()),None, c_int(canNOTIFY_RX))
def can_rx(self):
print("OK")
The function can_rx does execute only once. Any suggestions? Thank you very much.
MM
first thing:
The argtypes of ctypes WINFUCNTYPE or CFUNCTYPE should be a c_void_p
The argtypes of fucntion which you are defining should be py_object instead of a c_void_p
so that you can pass objects of structures and arrays as well.
then in the callback fucntion cast the void_p data as
ct.cast(userdata,ct.py_object).value
second thing:
code running only once: put a return value at bottom of the code.
return 1 or return TRUE
example:
CALLBACKFUNC = WINFUNCTYPE("returnvalue",c_int,c_void_p)
kvSetNotifyCallback.argtypes = [c_int,CALLBACKFUNC,py_object,c_uint]
then in the fucntion:
def can_rx(arg1,arg2):
arg3 = cast(arg2,py_object).value #yields object value
print arg1
print arg3
return 1 #for continuing execution
The CALLBACK FUNCTION can_rx should be called at CALLBACKFUNC(can_rx), the data to be passed along with arg1 should be passed at py_object position.
so you should call your callback as:
kvSetNotifyCallback(somevalue,CALLBACKFUC(can_rx),arg2,canNOTIFY_RX)
arg2 is the value to be passed to the callback func can_rx
can_rx can be defined as a global function also.