You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is this a "C++Weekly" or "The [Fill in the Blank] Programmer" episode request?
I think it would fit an C++Weekly episode much better.
Topics
Advanced template parameter packs.
Length
I think it's more bite-sized to quickly show this advanced functionality.
Content
Motivation
I stumbled across the problem of converting JNI types to "normal" C++ types.
The goal was that there are the same classes in Java and C++ with the same interfaces etc for simple understanding.
There obviously has to be some JNI binding layer between Java and C++ to convert between the Java types and the C++ types.
With the hand full of classes and dozens of methods each, it's against DRY to have the same conversions in every interface call.
The goal was to come up with a call method with a template parameter pack such that that the class instance could be passed as first argument, a class member function as second parameter and then all the JNI arguments in the parameter pack like this: call(instance, &MyClass::myMethod, arg1, arg2, arg3);
I don't think this is important to the content of the episode but explains why something like the following could be interesting at all.
Actual content
It's possible to unpack two parameter packs in parallel!
Here is an example of what I'm talking about. This a boiled down example of the example above (that doesn't make that much sense anymore).
#include <vector>
template<typename SRC, typename TGT>
TGT doConversion(SRC src);
template<>
int doConversion<double const&, int>(double const& arg) {
return static_cast<int>(arg);
}
template<>
std::vector<int> const& doConversion<std::vector<int> const&, std::vector<int> const&>(std::vector<int> const& arg) {
return arg;
}
template <typename TYPE, typename OUT, typename... FARGS, typename... ARGS>
OUT call(TYPE& obj, OUT (TYPE::*func)(FARGS...), ARGS const&... args) {
return (obj.*func)(doConversion<ARGS const&, FARGS>(args)...);
}
class S {
public:
int getCount(int a, std::vector<int> const& b) {
return 3 + b[0];
}
};
int main() {
S s;
return call(s, &S::getCount, 3.0, std::vector<int>{3, 4});
}
My point is that inside call there is the line return (obj.*func)(doConversion<ARGS const&, FARGS>(args)...); which unpacks ARGS and FARGS simultaneously. I wasn't aware of this possibility - it even took me a while to understand how one template can have two parameter packs.
The text was updated successfully, but these errors were encountered:
Channel
Is this a "C++Weekly" or "The [Fill in the Blank] Programmer" episode request?
I think it would fit an C++Weekly episode much better.
Topics
Advanced template parameter packs.
Length
I think it's more bite-sized to quickly show this advanced functionality.
Content
Motivation
I stumbled across the problem of converting JNI types to "normal" C++ types.
The goal was that there are the same classes in Java and C++ with the same interfaces etc for simple understanding.
There obviously has to be some JNI binding layer between Java and C++ to convert between the Java types and the C++ types.
With the hand full of classes and dozens of methods each, it's against DRY to have the same conversions in every interface call.
The goal was to come up with a
call
method with a template parameter pack such that that the class instance could be passed as first argument, a class member function as second parameter and then all the JNI arguments in the parameter pack like this:call(instance, &MyClass::myMethod, arg1, arg2, arg3);
I don't think this is important to the content of the episode but explains why something like the following could be interesting at all.
Actual content
It's possible to unpack two parameter packs in parallel!
Here is an example of what I'm talking about. This a boiled down example of the example above (that doesn't make that much sense anymore).
My point is that inside
call
there is the linereturn (obj.*func)(doConversion<ARGS const&, FARGS>(args)...);
which unpacksARGS
andFARGS
simultaneously. I wasn't aware of this possibility - it even took me a while to understand how one template can have two parameter packs.The text was updated successfully, but these errors were encountered: