简体   繁体   中英

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

};

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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