How does the compiler determine between a function using SFINAE and a standard function if both are viable?

前端 未结 2 1127
有刺的猬
有刺的猬 2020-12-21 01:56

Consider the following code:

#include 
#include 

template 
class A
{
public:
    // Allow func to be ca         


        
相关标签:
2条回答
  • 2020-12-21 02:28

    The rule is that if a non function template and a function template specialization have the same signature, then the non function template is chosen over the template specialization. This can be found in [over.match.best]/2

    Given these definitions, a viable function F1 is defined to be a better function than another viable function F2 if for all arguments i, ICSi(F1) is not a worse conversion sequence than ICSi(F2), and then

    [...]

    • F1 is not a function template specialization and F2 is a function template specialization [...]
    0 讨论(0)
  • 2020-12-21 02:42

    You can read about overload resolution here. In particular

    If any candidate is a function template, its specializations are generated using template argument deduction, and such specializations are treated just like non-template functions except where specified otherwise in the tie-breaker rules.

    And then

    Best viable function

    For each pair of viable function F1 and F2, the implicit conversion sequences from the i-th argument to i-th parameter are ranked to determine which one is better [...]

    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 [...]

    4) [...] F1 is a non-template function while F2 is a template specialization

    Basically the same rules apply in this simpler example:

    #include<iostream>
    
    template <typename T> 
    void foo(T i) { std::cout << "template" ; }
    
    void foo(int i) { std::cout << "non template"; }
    
    int main() {
        foo(1);   // non template
    }
    
    0 讨论(0)
提交回复
热议问题