简体   繁体   English

为什么必须在类外使用template <>对成员模板方法进行专门化

[英]Why member template method have to be specialized using template<> outside of class

I am not clear of when and how template functions are created by compiler. 我不清楚编译器何时以及如何创建template函数。 So I cannot explain the behavior of following 2 examples. 因此,我无法解释以下两个示例的行为。

Example 1. 范例1。

struct C1 {                                                    
    template <typename T>
    void g(T t);
};

template<>   
void C1::g(double x) {
    cout << "Member templates. C1::g(double) " << x << endl;
}

The above code builds and runs. 上面的代码生成并运行。 However, without the template<> , g++ complains. 但是,如果没有template<> ,则g ++会抱怨。

error: prototype for ‘void C1::g(double)’ does not match any in class ‘C1’

But it is OK if I place the definition of g(double ) inside the class. 但是如果我将g(double)的定义放到类中就可以了。

Question 1: Why member template method have to be specialized using template<> outside of class? 问题1:为什么必须在类之外使用template <>对成员模板方法进行专门化处理?

Example 2. 示例2

struct C1 {                                                          
    template <typename T>
    void g(T t);

    void g(double x) {
        cout << "C1::g(double): " << x << endl;
    }
};

template<>
void C1::g(double x) {
    cout << "Member templates. C1::g(double) " << x << endl;
}

C1 c;
c.g(10.5); // output: C1::g(double): 10.5

The member template template void g() is not called. 未调用成员模板模板void g()。 Which makes me wonder 这让我纳闷

Question 2. Does the member template ever gets specialized? 问题2.成员模板是否曾经专门化?

The member template template void g() is not called. 未调用成员模板模板void g()。 Which makes me wonder 这让我纳闷

It's function overloading issue here, and the non-template function will win a template specialization in overloading resolution. 这是函数重载的问题,非模板函数将赢得重载分辨率方面的模板专长。

http://en.cppreference.com/w/cpp/language/overload_resolution http://en.cppreference.com/w/cpp/language/overload_resolution

Best viable function 最佳可行功能

For each pair of viable function F1 and F2, the implicit conversion sequences from the i-th parameter to i-th argument are ranked to determine which one is better (except the first argument, the implicit object argument for static member functions has no effect on the ranking) 对于每对可行函数F1和F2,对从第i个参数到第i个参数的隐式转换序列进行排序,以确定哪个更好(除了第一个参数,静态成员函数的隐式对象参数无效)在排名上)

F1 is determined to be a better function than F2 if implicit conversions for all arguments of F1 are not worse than the implicit conversions for all arguments of F2, and 如果F1的所有自变量的隐式转换不比F2的所有自变量的隐式转换差,则F1被确定为比F2更好的函数,并且

1) there is at least one argument of F1 whose implicit conversion is better than the corresponding implicit conversion for that argument of F2 1)F1至少有一个参数,其隐式转换要好于F2的相应隐式转换

2) or. 2)或。 if not that, (only in context of non-class initialization by conversion), the standard conversion sequence from the return type of F1 to the type being initialized is better than the standard conversion sequence from the return type of F2 如果不是,(仅在通过转换进行非类初始化的情况下),从F1的返回类型到要初始化的类型的标准转换顺序要好于F2的返回类型的标准转换顺序

3) or, if not that, F1 is a non-template function while F2 is a template specialization 3),或者如果不是,则F1是非模板函数,而F2是模板特化

4) or, if not that, F1 and F2 are both template specializations and F1 is more specialized according to the partial ordering rules for template specializations 4),或者如果不是,则F1和F2都是模板专业化,而F1根据模板专业化的部分排序规则更专业化

You can call the template specialization function explicitly: 您可以显式调用模板专用化功能:

c.g<>(10.5); // output: Member templates. C1::g(double) 10.5

LIVE 生活

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

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