Use C ++ 11 to implement C ++ 17 apply (Dynamic Array Used as function parameter) and apply Array

Source: Internet
Author: User

Use C ++ 11 to implement C ++ 17 apply (Dynamic Array Used as function parameter) and apply Array

The title is incorrect. apply uses tuple as the parameter to call a function. This title is used to better adapt to search keywords.

Dynamic Arrays used as function parameters are more suitable for C ++ programs embedded in the script environment, such as lua or javascript (js ).

If you have any mistakes or improvement, please comment. Thank you.

Although VS2017 has implemented some C ++ 17 features, it does not apply (maybe I did not find or replace it), and even if it is added later, it does not meet the list of parameters for converting arrays I mentioned.

The following is the code that passes the VS2015.3 test.

The write script encapsulation (Wrapper) function generally registers C ++ functions (generally member functions) to the script environment, I have read that many open-source authors have reloaded many template classes/template functions. In fact, they all work very well. Although they all work in a certain way, they are modified in batches.

This article references the reply from Johannes Schaub in stackoverflow. The appendix has a link.

The intint in the Code is just an example of automatic conversion, and nothing is done. You can replace it with the converter for converting your script object to the native object.

The core part of the code is nested template class inheritance, which is quite confusing:

Template <int...> struct seq {}; template <int N, int... s> struct gen_seq: gen_seq <N-1, N-1, S...> {}; // nested inheritance, in order to get a parameter sequence from N-M to N, is an infinite template <int... s> struct gen_seq <1, S...> {typedef seq <S...> type ;}; // special case. Termination of the nested inheritance above. The termination condition is from 1 to N.

 

All code, no output. Please add it yourself. Similarly, no other header files are required:

struct intint {    int i;    intint(int i) :i(i) {}    operator int() { return i; }};template<int ...>struct seq {};template<int N, int ...S> struct gen_seq : gen_seq<N - 1, N - 1, S...> {};template<int ...S> struct gen_seq<1, S...> { typedef seq<S...> type; };template<typename T, typename R, typename...TS>struct callable {    typename gen_seq<sizeof...(TS)+1>::type fo;    R(T::*func)(TS...);    callable(R(T::*func)(TS...)) :func(func) {}    template<int ...S>    void call(seq<S...>, int* v) {        (new T->*func)(intint(v[S])...);    }    void operator()(T*, int* v) {        call(fo, v);    }};struct foo{    void func(int, int, int, int) {    }};int main(){    callable<foo, void, int, int, int, int> c(&foo::func);    int v[] = { 100,200,300,400,500,600 };    c(new foo(), v);    return 0;}

 

 

 

Appendix: ["unpacking" a tuple to call a matching function pointer]

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.