具有 const 类型或简单类型参数的方法的 C++ 模板函数

C++ template function for method with a parameter of a const type or simply of type

假设我有两个用于调用 class 方法的模板函数的相同主体,如下所示:

 template <typename jsType, typename jsParamType, typename ParamPrivateType = jsParamType::PrivateType, void(PrivateType::*Method)(const ParamPrivateType&)>
    static bool SetByRefMethod(JSContext *cx, unsigned argc, JS::Value *vp)
{
...
}

 template <typename jsType, typename jsParamType, typename ParamPrivateType = jsParamType::PrivateType, void(PrivateType::*Method)(      ParamPrivateType&)>
    static bool SetByRefMethod(JSContext *cx, unsigned argc, JS::Value *vp)
{
...
}

我尝试只写一次正文,那么当调用的方法有一个参数 const 而当它有一个参数 时,让编译器使用它的正确方法是什么? const?

您不需要对所有这些类型进行模板化。只有第一个会做。如果您可以使用第一个模板化类型推断出其余类型,那么您应该没问题。如果可以从第一个模板类型中推导出私有类型,则不需要模板中的私有类型。看看下面的代码

#include <iostream>
using std::cout;
using std::endl;
#include <vector>
using std::vector;

class A {
public:
    using ClassType = int;

    void print_const_integer(const int& a) {
        cout << "A's member function accepting a const ref called" << endl;
        cout << a << endl;
    }

    void print_integer(int& a) {
        cout << "A's member function accepting a ref called" << endl;
        cout << a << endl;
    }

};
class B {
public:
    using ClassType = int;

    void print_integer(int& a) {
        cout << "B's member function called" << endl;
        cout << a << endl;
    }
};

template <typename TYPE>
void function(TYPE object, void (TYPE::*member_func_pointer)(int&)) {
    typename TYPE::ClassType a = 1;
    (object.*member_func_pointer)(a);
}

template <typename TYPE>
void function(TYPE object, void (TYPE::*member_func_pointer)(const int&)) {
    typename TYPE::ClassType a = 1;
    (object.*member_func_pointer)(a);
}

int main() {
    A a;
    B b;

    function(a, &A::print_const_integer);
    function(a, &A::print_integer);
    function(b, &B::print_integer);

    return 0;
}

希望对您有所帮助!

这是一个解决方案,但是,还有更好的方法吗?

#include <iostream>
using std::cout;
using std::endl;
#include <vector>
using std::vector;
#include <type_traits>

class A {
public:
    using ClassType = int;

    void print_const_integer(const int& a) {
        cout << "A's member function accepting a const ref called" << endl;
        cout << a << endl;
    }

    void print_integer(int& a) {
        cout << "A's member function accepting a ref called" << endl;
        cout << a << endl;
    }

};
class B {
public:
    using ClassType = int;

    void print_integer(int& a) {
        cout << "B's member function called" << endl;
        cout << a << endl;
    }
};

template <typename TYPE, void(TYPE::*member_func_pointer)(int&)>
void function(TYPE object) {
    typename TYPE::ClassType a = 1;
    (object.*member_func_pointer)(a);
}

template <typename TYPE, void(TYPE::*member_func_pointer)(const int&)>
void function(TYPE object) {
    typename TYPE::ClassType a = 1;
    (object.*member_func_pointer)(a);
}


template <typename TYPE, typename FUNCTYPE>
 struct opt_const_param : std::enable_if<std::is_same<FUNCTYPE, void(TYPE::*)(      int&)>::value
                                      || std::is_same<FUNCTYPE, void(TYPE::*)(const int&)>::value, FUNCTYPE>
{
};

template <typename TYPE, typename FUNCTYPE, typename opt_const_param<TYPE, FUNCTYPE>::type member_func_pointer>
void function2(TYPE object) {
    typename TYPE::ClassType a = 1;
    (object.*member_func_pointer)(a);
}

int main() {
    A a;
    B b;

    function2<A, void(A::*)(const int&), &A::print_const_integer>(a);
    function2<A, void(A::*)(int&), &A::print_integer>(a);
    function2<B, void(B::*)(int&), &B::print_integer>(b);

    return 0;
}

我这里有另一种方法,它看起来更适合与宏一起使用:

#include <iostream>
using std::cout;
using std::endl;
#include <vector>
using std::vector;
#include <type_traits>

class A {
public:
    using ClassType = int;

    void print_const_integer(const int& a) {
        cout << "A's member function accepting a const ref called" << endl;
        cout << a << endl;
    }

    void print_integer(int& a) {
        cout << "A's member function accepting a ref called" << endl;
        cout << a << endl;
    }

};
class B {
public:
    using ClassType = int;

    void print_integer(int& a) {
        cout << "B's member function called" << endl;
        cout << a << endl;
    }
};

template <typename TYPE, typename FUNCTYPE>
 struct opt_const_param_function3 : std::enable_if<std::is_same<FUNCTYPE, void(TYPE::*)(      int&)>::value
                                      || std::is_same<FUNCTYPE, void(TYPE::*)(const int&)>::value, FUNCTYPE>
{
};

template <typename TYPE, typename FUNCTYPE, typename opt_const_param_function3<TYPE, FUNCTYPE>::type member_func_pointer> //FUNCTYPE member_func_pointer>
void function3(TYPE object) {
    typename TYPE::ClassType a = 1;
    (object.*member_func_pointer)(a);
}

template <typename TYPE, void(TYPE::*member_func_pointer)(int&)>
void function3(TYPE object) {
    function3<TYPE, decltype(member_func_pointer), member_func_pointer>(object);
}

template <typename TYPE, void(TYPE::*member_func_pointer)(const int&)>
void function3(TYPE object) {
    function3<TYPE, decltype(member_func_pointer), member_func_pointer>(object);
}

int main() {
    A a;
    B b;

    function3<A, &A::print_const_integer>(a);
    function3<A, &A::print_integer>(a);
    function3<B, &B::print_integer>(b);

    return 0;
}

有什么想法吗?