简体   繁体   English

类方法的部分特化

[英]Partial specialization for class method

I'm trying to specialize a method of a (non-templated!) class.我正在尝试专门化(非模板化!)类的方法。 Apparently, it's not possible, however I am struggling to figure out why, or how to overcome the problem.显然,这是不可能的,但是我正在努力找出原因或如何克服这个问题。

class MyClass {

public:
        template <typename... T>
        auto MyMethod(T... t) -> void { std::cout << "Original" << std::endl; }

        template <typename... T>
        auto MyMethod<int, T...>(int value, T... t) -> void { std::cout << "Specialization" << value << std::endl; }

};

int main(void) {
        MyClass myClass;
        myClass.MyMethod<char, char>('c', 'c');
        myClass.MyMethod<int, char>(123, 'c');

        return 0;
}

Only classes can be partially<\/em> specialized;只有类可以部分<\/em>特化; methods can only be fully<\/em> specialized.方法只能完全<\/em>专业化。 As your methods still have template arguments ( T<\/code> ) that are not specified, this means a partial method specialization.由于您的方法仍然具有未指定的模板参数 ( T<\/code> ),这意味着部分方法特化。

If you would use these template arguments for a class (and call a non-templated member function of that class) then this should be possible.如果您将这些模板参数用于一个类(并调用该类的非模板化成员函数),那么这应该是可能的。

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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