user2274361
user2274361

Reputation: 47

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

Upvotes: 1

Views: 201

Answers (3)

user995502
user995502

Reputation:

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

Upvotes: 0

mickeyt
mickeyt

Reputation: 406

Try this for your template definition:

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

Upvotes: 0

Andy Prowl
Andy Prowl

Reputation: 126432

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::strings 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.

Upvotes: 4

Related Questions