将模板化类型传递给 C++ 中的成员函数

Pass a templatized type to a member function in C++

本文关键字:C++ 成员 函数 类型      更新时间:2023-10-16

我正在尝试编写一个成员函数,该函数可以实例化自定义类型(模板化)的对象,将其const&成员初始化为函数的本地对象。
这是一致的,因为自定义类型对象的生存期与local_object相同。

目标是缓存本地对象的某些元数据,因为它们在其生存期内不会更改。operator()(或任何成员函数)计算一些值,然后在func后面使用,目标是提供一个钩子来改变func的行为。

由于(剖析)缓慢,请不要使用多态溶液(当前使用)。

这是一个M(N)我们:

#include <vector>
class cls {
public:
    template <typename Custom> int func() {
        std::vector<int> local_object{0, 14, 32};
        Custom c(local_object, 42);
        return c();
    }
};
template<typename AType> class One {
public:
    One(const AType& obj, const int n): objref(obj), param(n), member_that_should_depend_on_objref(obj.size()) {}
    int operator()() { return 42; }
private:
    const AType& objref;
    const int param;
    float member_that_should_depend_on_objref;
};
template<typename AType> class Two {
public:
    Two(const AType& obj, const int n): objref(obj), param(n), other_member_that_should_depend_on_objref(obj.empty()), other_dependent_member(obj.back()) {}
    int operator()() { return 24; }
private:
    const AType& objref;
    const int param;
    bool other_member_that_should_depend_on_objref;
    int other_dependent_member;
};
int main() {
    cls myobj;
    auto a = myobj.func<One>();
    auto b = (myobj.func<Two>)();
}

G++ 5.3.0 说

tmp.cpp: In function 'int main()':
tmp.cpp:34:30: error: no matching function for call to 'cls::func()'
     auto a = myobj.func<One>();
                              ^
tmp.cpp:4:36: note: candidate: template<class Custom> int cls::func()
     template <typename Custom> int func() {
                                    ^
tmp.cpp:4:36: note:   template argument deduction/substitution failed:
tmp.cpp:35:32: error: no matching function for call to 'cls::func()'
     auto b = (myobj.func<Two>)();
                                ^
tmp.cpp:4:36: note: candidate: template<class Custom> int cls::func()
     template <typename Custom> int func() {
                                    ^
tmp.cpp:4:36: note:   template argument deduction/substitution failed:

叮当当 3.7.1 说:

tmp.cpp:34:20: error: no matching member function for call to 'func'
    auto a = myobj.func<One>();
             ~~~~~~^~~~~~~~~
tmp.cpp:4:36: note: candidate template ignored: invalid explicitly-specified argument for template
      parameter 'Custom'
    template <typename Custom> int func() {
                                   ^
tmp.cpp:35:21: error: no matching member function for call to 'func'
    auto b = (myobj.func<Two>)();
             ~~~~~~~^~~~~~~~~~
tmp.cpp:4:36: note: candidate template ignored: invalid explicitly-specified argument for template
      parameter 'Custom'
    template <typename Custom> int func() {
                                   ^
2 errors generated.
auto a = myobj.func<One>();

是错误的,因为One是类模板,而不是类。 用途

auto a = myobj.func<One<SomeType>>();

从您的代码中不清楚SomeType应该是什么。

更新

如果要使用:

auto a = myobj.func<One>();

您需要更改func以使用模板模板参数:

class cls {
public:
    template <template <class> class Custom > int func() {
        std::vector<int> local_object{0, 14, 32};
        Custom<std::vector<int>> c(local_object, 42);
        return c();
    }
};

也许这就是你的意图。