简体   繁体   中英

Default template argument for various types

I am trying to create a program , where I have to use templates and I am kind of stuck for a while now. Here is a part of my code:

    template <typename _Type, typename _Comparator = equal_to</*char*/> >
        class CSearch {
public:
    CSearch();
    CSearch(const _Comparator & cmp);
    void Add(int id,
            const _Type & needle);
    set<int> Search(const _Type & hayHeap) const;
private:

    struct CSElem {
        int id;
        _Type sekvence;
    };
    vector<CSElem> data;
    _Comparator comp;
};

template <typename _Type, typename _Comparator>
CSearch<_Type, _Comparator>::CSearch() {
    comp = _Comparator();
}

.......

template <typename _Type, typename _Comparator>
void CSearch<_Type, _Comparator>::Add(int id, const _Type& needle) {
    CSElem temp;

    .....

    data.push_back(temp);
}

template <typename _Type, typename _Comparator>
set<int> CSearch<_Type, _Comparator>::Search(const _Type& hayHeap) const {
typename _Type::const_iterator j, t;

   ...... //trying to find out which items of the variable data are part of hayHeap

                    if (comp(*t, *j)) {
                        ......
                    }

   ......

}

/*
 *
 */
int main(int argc, char** argv) {
    CSearch <string> test1;
    test1 . Add(0, "hello");
    test1 . Add(1, "world");
    test1 . Search("hello world!");

    CSearch <vector<int> > test2;

....

}

So the problem is that when I do not supply the second parameter to the template, the comparator should be equal_to for the type that is stored in the test variables so for string it should be

equal_to<char>

or for vector of ints

equal_to<int>

I have been thinking about it for a long while and still haven't figured out how to declare the template or what else to do so it has the previously mentioned functionality. I would be very happy if someone could give me a hint or an example of how to solve this.

Thanks

You can use previous template parameters in a template signature to form default arguments for a subsequent template parameter. Also, you can exploit the fact that all standard containers define the value_type alias for the type of the contained elements:

template <typename _Type, 
          typename _Comparator = equal_to<typename _Type::value_type> >
//                                                      ^^^^^^^^^^^^

std::string s also have member functions and type aliases that allow them to be used by generic algorithms of the C++ Standard Library, so the above should work also when _Type is std::string .

试试这个模板定义:

template <typename _Type, typename _Comparator = equal_to<_Type> >

You can also also make _ Container a template template parameter.

template <typename _Type, template <typename> class _Comparator = equal_to > 
class CSearch {
public:
    typedef typename _Type::value_type elem_type;
    CSearch();
    CSearch(const _Comparator<elem_type> & cmp);
    void Add(int id,
            const _Type & needle);
    set<int> Search(const _Type & hayHeap) const;
private:

    struct CSElem {
        int id;
        _Type sekvence;
    };
    vector<CSElem> data;
    _Comparator<elem_type> comp;
};

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