Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
87 views
in Technique[技术] by (71.8m points)

c++ - How to use a template member function as an argument to another template member function?

So I'm experimenting with templates and probably I have a syntax problem somewhere. I'm trying to write a class, which takes a vector of selected items, converts their properties to boolean arrays, and then generates object which represent the state of the whole selection> This object is sent to the UI view and shown on a context menu with tri-state checkboxes. I know how to write this the long and hard way, but I want to avoid code duplication, so I decided to go with templates. The problem is that I can't pass a member function to another member function. I'm getting compile error C3867 non-standard syntax; use '&' to create a pointer to member.

First: Sorry, if it is a duplicate. Second: sorry for the long example, but I have no idea where my mistake is.

#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;
    }

};
question from:https://stackoverflow.com/questions/65924119/how-to-use-a-template-member-function-as-an-argument-to-another-template-member

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Well, you really are missing & and the names have to be prepended with the class name. Try this:

models.general = getCheckStateFromAnyStatus(selectedItems, &CheckModelGenerator::getGeneralStatus);
models.surface = getCheckStateFromAnyStatus(selectedItems, &CheckModelGenerator::getSurfaceStatus);
models.other = getCheckStateFromAnyStatus(selectedItems, &CheckModelGenerator::getSomeOtherStatus);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...