如何使用模板成员函数作为另一个模板成员函数的参数?
How to use a template member function as an argument to another template member function?
所以我正在试验模板,可能我在某个地方遇到了语法问题。我正在尝试编写一个 class,它采用所选项目的向量,将它们的属性转换为布尔数组,然后生成代表整个选择状态的对象>该对象被发送到 UI 查看并显示在带有三态复选框的上下文菜单上。我知道如何以漫长而艰难的方式编写它,但我想避免代码重复,所以我决定使用模板。问题是我不能将一个成员函数传递给另一个成员函数。我收到编译错误 C3867 非标准语法;使用“&”创建指向成员的指针。
首先:抱歉,如果是重复的。第二:对不起,这个长例子,但我不知道我的错误在哪里。
#include <array>
#include <vector>
#include "Item.h"
enum class CheckState {unchecked, checked, partially_checked}; //the state of the context menu checkboxes
struct CheckBoxModels //this is the final product we want to get
{
std::array<CheckState, 10> general;
std::array<CheckState, 6> surface;
std::array<CheckState, 7> other;
};
class CheckModelGenerator{ //this class has to generate the chechBoxModel by taking a vector of pointers to the selected Items;
std::array<bool, 10> getGeneralStatus(const Item& item); //each of these knows how to get a specific list of properties from an item
std::array<bool, 6> getSurfaceStatus(const Item& item); //and represent it with boolean
std::array<bool, 7> getSomeOtherStatus(const Item& item);//depending on whether they exist or not;
template<int Size> //using this to call ANY of the functions above
using getAnyStatus = std::array<bool, Size>(CheckModelGenerator::*) (const Item& item);
template<int size> //a member function that converts the bool arrays to CheckState arrays, by iterrating trough the selected items
std::array<CheckState, size> getCheckStateFromAnyStatus(const std::vector<Item*>& selectedItems, getAnyStatus<size> getStatus);
public:
CheckBoxModels getAllCheckBoxModels(std::vector<Item*>& selectedItems) // this here creates the final product;
{
CheckBoxModels models;
models.general = getCheckStateFromAnyStatus(selectedItems, getGeneralStatus); //I'm having problem actually calling those template functions!
models.surface = getCheckStateFromAnyStatus(selectedItems, getSurfaceStatus);
models.other = getCheckStateFromAnyStatus(selectedItems, getSomeOtherStatus);
return models;
}
};
好吧,您确实缺少 &
并且必须在名称前加上 class 名称。试试这个:
models.general = getCheckStateFromAnyStatus(selectedItems, &CheckModelGenerator::getGeneralStatus);
models.surface = getCheckStateFromAnyStatus(selectedItems, &CheckModelGenerator::getSurfaceStatus);
models.other = getCheckStateFromAnyStatus(selectedItems, &CheckModelGenerator::getSomeOtherStatus);
所以我正在试验模板,可能我在某个地方遇到了语法问题。我正在尝试编写一个 class,它采用所选项目的向量,将它们的属性转换为布尔数组,然后生成代表整个选择状态的对象>该对象被发送到 UI 查看并显示在带有三态复选框的上下文菜单上。我知道如何以漫长而艰难的方式编写它,但我想避免代码重复,所以我决定使用模板。问题是我不能将一个成员函数传递给另一个成员函数。我收到编译错误 C3867 非标准语法;使用“&”创建指向成员的指针。
首先:抱歉,如果是重复的。第二:对不起,这个长例子,但我不知道我的错误在哪里。
#include <array>
#include <vector>
#include "Item.h"
enum class CheckState {unchecked, checked, partially_checked}; //the state of the context menu checkboxes
struct CheckBoxModels //this is the final product we want to get
{
std::array<CheckState, 10> general;
std::array<CheckState, 6> surface;
std::array<CheckState, 7> other;
};
class CheckModelGenerator{ //this class has to generate the chechBoxModel by taking a vector of pointers to the selected Items;
std::array<bool, 10> getGeneralStatus(const Item& item); //each of these knows how to get a specific list of properties from an item
std::array<bool, 6> getSurfaceStatus(const Item& item); //and represent it with boolean
std::array<bool, 7> getSomeOtherStatus(const Item& item);//depending on whether they exist or not;
template<int Size> //using this to call ANY of the functions above
using getAnyStatus = std::array<bool, Size>(CheckModelGenerator::*) (const Item& item);
template<int size> //a member function that converts the bool arrays to CheckState arrays, by iterrating trough the selected items
std::array<CheckState, size> getCheckStateFromAnyStatus(const std::vector<Item*>& selectedItems, getAnyStatus<size> getStatus);
public:
CheckBoxModels getAllCheckBoxModels(std::vector<Item*>& selectedItems) // this here creates the final product;
{
CheckBoxModels models;
models.general = getCheckStateFromAnyStatus(selectedItems, getGeneralStatus); //I'm having problem actually calling those template functions!
models.surface = getCheckStateFromAnyStatus(selectedItems, getSurfaceStatus);
models.other = getCheckStateFromAnyStatus(selectedItems, getSomeOtherStatus);
return models;
}
};
好吧,您确实缺少 &
并且必须在名称前加上 class 名称。试试这个:
models.general = getCheckStateFromAnyStatus(selectedItems, &CheckModelGenerator::getGeneralStatus);
models.surface = getCheckStateFromAnyStatus(selectedItems, &CheckModelGenerator::getSurfaceStatus);
models.other = getCheckStateFromAnyStatus(selectedItems, &CheckModelGenerator::getSomeOtherStatus);