简体   繁体   English

C++ 模板参数作为函数调用名称

[英]C++ template parameter as function call name

In C++, is possible to make something like this:在 C++ 中,可以做这样的事情:

class A 
{
public:
   template <typename ClassType, typename MethodName, typename ...Args>
   static func()
   {
       ClassType t = GetPtr<ClassType>();
       t->MethodName(GetArg<Args>()...);
   }
}

A::func<B, sum, double, double>(); //should call B->sum(double, double)
A::func<C, sum2, std::string, double, double>(); //should call C->sum2(std::string, double, double)

where GetPtr and GetArgs are working methods to obtain pointers from dictionary.其中GetPtrGetArgs是从字典中获取指针的工作方法。

You could make use of non-type template parameters like:您可以使用非类型模板参数,例如:

template <typename ClassType, typename MethodType, MethodType MethodName, typename ...Args>
static void func() {
    ClassType t = GetPtr<ClassType>();
    (t->*MethodName)(GetArg<Args>()...);
}

to use it as follows:使用它如下:

A::func<B, double (B::*)(double,double), &B::sum, double, double>();

When your compiler will support c++17 your code could get even shortened:当您的编译器支持 c++17 时,您的代码甚至可以缩短:

template <typename ClassType, auto MethodName, typename ...Args>
static void func() {
    ClassType t = GetPtr<ClassType>();
    (t->*MethodName)(GetArg<Args>()...);
}

With usage:使用情况:

A::func<B, &B::sum, double, double>();

Not exactly what you asked, but I suppose you can do someting like不完全是你问的,但我想你可以做一些像

#include <array>
#include <vector>
#include <iostream>

template <typename Class, typename Method, typename ... Args>
void callMethod (Class & c, Method m, Args ... args)
 { ((&c)->*m)(args...); }

int main()
 {
   std::vector<int>     v;
   std::array<int, 5U>  a;

   callMethod(v, &std::vector<int>::size);
   callMethod(v, static_cast<void (std::vector<int>::*)(int const &)>
                    (&std::vector<int>::push_back), 7);
   callMethod(a, &std::array<int, 5U>::fill, 3);

   std::cout << v.back() << std::endl; // print "7"

   for ( auto const i : a )
      std::cout << i << std::endl; // print "3" five times
 }

Observe that with push_back() method of std::vector<int> , you need a cast ( static_cast<void (std::vector<int>::*)(int const &)> ) to resolve the overloading ambiguity (there are two push_back() in std::vector<int> )观察到,使用std::vector<int> push_back()方法,您需要一个static_cast<void (std::vector<int>::*)(int const &)>static_cast<void (std::vector<int>::*)(int const &)> )来解决重载歧义(有是std::vector<int>中的两个push_back() )

Not sure about the use of GetPtr() and GetArgs() , but I suppose you can use callMethod() as follows不确定GetPtr()GetArgs() ,但我想你可以使用callMethod()如下

callMethod(GetPtr<B>(), &B::sum, GetArgs<double>(),
           GetArgs<double>());
callMethod(GetPtr<C>(), &C::sum2, GetArgs<std::string>(),
           GetArgs<double>(), GetArgs<double>());

as the word typename suggests, no, this won't work (generally), but:正如typename暗示的那样,不,这不起作用(通常),但是:

you can basically just create a class that overloads operator(.) , and do the calling inside the overloaded operator.您基本上可以创建一个重载operator(.) ,并在重载的运算符内部进行调用。

In fact, Boost::bind does exactly that and allows you to get "functor" objects that you can pass around at runtime as objects.事实上, Boost::bind正是这样做的,并允许您获得可以在运行时作为对象传递的“函子”对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM