Class specialization versus function overloading

I thought that I needed a specialized function for the template, but https://stackoverflow.com/a/2128321/2168168 makes me think that I really have to perform the overload function, However, I just don’t see how I could achieve that what I want.

I managed to achieve the goal with the specialization of the class template, but I do not like the fact that I have so much replicated code between the template class and the specialized class.

I have a class, among other things, two keys that are used to sort the objects of a class. In addition, I want to create a method match()that will return true for the string if the original part of the string matches (that is, "aaa" will match "aaa: zzz", because the first three characters of both lines are "aaa",), but ints shorts etc. will only match if it is an exact match (i.e. 1 == 1).

I got this to work using class specialization, as shown below:

template <class    KEY2_TYPE>
class policy_key_c
{
public:

    policy_key_c (int          _key1,
                  KEY2_TYPE    _key2) :
        key1(_key1),
        key2(_key2)
        {};


    virtual ~policy_key_c(void) {};


    virtual std::string strIdx (void) const {
        // combine key1 and key2 into an index to be returned.
    }


    //
    // operator <
    //
    virtual bool operator< (const policy_key_c    &b) const {
        return (operator<(&b));
    }


    virtual bool operator< (const policy_key_c    *p) const {

        // if the primary key is less then it less, don't check 2ndary
        if (key1 < p->key1) {
            return (true);
        }


        // if not less then it >=, check if equal, if it not equal then it
        // must be greater
        if (!(key1 == p->key1)) {
            return (false);
        }

        // its equal to, so check the secondary key
        return (key2 < p->key2);
    }



    //
    // operator ==
    //
    virtual bool operator== (const policy_key_c    &b) const {
        return(operator==(&b));
    }


    virtual bool operator== (const policy_key_c    *p) const {

        // if the primary key isn't equal, then we're not equal
        if ((key1 != p->key1)) {
            return (false);
        }

        // primary key is equal, so now check the secondary key. 
        return (key2 == p->key2);
    }


    //
    // match
    //
    virtual bool match (const policy_key_c    &b) const {
        return(operator==(&b));
    }


    virtual bool match (const policy_key_c    *p) const {
        return (operator==(p));
    }


protected:

    int          key1;    // The primary key
    KEY2_TYPE    key2;    // The secondary key.
   // ... other class data members ....
};




// Now specialize the template for a string as the secondary key
//
template <>
class policy_key_c<std::string>
{
public:
    //
    // .... all the other functions
    //

    //
    // match
    //
    virtual bool match (const policy_key_c    &b) const {
        return(operator==(&b));
    }


    virtual bool match (const policy_key_c    *p) const {
        // do a prefix string match rather than a complete match.
        return (key2.substr(0, p->key2.lenght()) == p->key2);
    }


protected:

    int            key1;    // The primary key
    std::string    key2;    // The secondary key.
   // ... other class data members ....
};

I don't like this solution because there is so much replicated code. The only thing that behaves differently is the coincidence function. When key2 is an int, short or char match, it behaves like == wherease, if key2 is std :: string, I want it to do a prefix match.

? ? , , . .

.


10/12/10

PigBen , . , . , .

template <int KEY1_VAL, class KEY2_TYPE> class policy_key_c

typdefs, :

typedef    policy_key_c<1, int>           int_policy;
typedef    policy_key_c<2, std::string>   str_policy;

, , , .


10/12/10

PigBen , .

, , , . . , - ( ).

+3
3

, -, - , , . , , , , :

template <class T>
class X
{
    void f();
};

template <class T>
void X<T>::f()
{
    // general code
}

template<>
void X<std::string>::f()
{
    // specialized code
}

template<int K, typename T> class X;
template<int K, typename T> void friend_func(X<K,T> &);

template<int K, typename T>
class X
{
public:
    void class_func();
    friend void friend_func<>(X &);
};

template<int K, typename T>
void X<K,T>::class_func()
{
    friend_func(*this);
}

template<int K, typename T>
void friend_func(X<K,T> & x)
{
    // non specialized version
}

template<int K>
void friend_func(X<K,std::string> & x)
{
    // specialized version
}
+3

- match, ( C qsort). . , , C-ish , .

+2

match() . :

// primary template for general-purpose matching
template <typename T>
struct match_impl
{
    static bool match(const T& x) { return true; }
};

// specialization for std::string matching
template <>
struct match_impl<std::string>
{
    static bool match(const std::string& x) { return true; }
};

template <typename T>
bool match(const T& x)
{
    return match_impl<T>::match(x);
}
+1

Source: https://habr.com/ru/post/1769064/


All Articles