简体   繁体   中英

Template class with template method specialized for itself

Lets say I have template class with template function. Example:

template<typename T>
class example
{
    T some_member;

    template<typename X>
    example& foo(X& val)
    {
        /* general stuff */
        return *this;
    }

    // rest of class
};

What I'm failing to achieve is to make specialization for this method which takes as argument object of type example < T > (same type as caller's). Little example to show how it suppose to work:

example<int> exampleObj;
example<int> sameTypeObj;
int diffrentType1;
example<double> diffrentType2;

exampleObj.foo(diffrentType1); // general template used
exampleObj.foo(diffrentType2); // general template used
exampleObj.foo(sameTypeObj); // specialization used

You can overload, but you cannot specialize, since there is no partial template function specialization.

example& foo(example&)
{
}

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