C++ templated function overloading rules

前端 未结 2 2015
我在风中等你
我在风中等你 2021-01-13 01:53

When overloading a templated function, how should the compiler chose which version of the function to call if it has the option to either:

  • Call a templated ver
2条回答
  •  醉梦人生
    2021-01-13 02:33

    In order to be able to get anything done, the standard invented a list of goodness, and the order in which different things would be tried. There are series of lectures on C9 from STL that go into this in length.

    First, the template instantiates a solution for each of the three cases. The second version picks func(Parent) because it matches exactly, and wins over the template. The third call takes the templated version over a conversion which is considered "less good".

    My only thought on preventing this would be to do some horrible SFINAE that tests for every T that it can't be inherited from Parent using type traits. Concepts in C++17 might allow for something slightly less convoluted.

    See

    Stephan T. Lavavej: Core C++, 2 of n - Template Argument Deduction

    Stephan T. Lavavej: Core C++, 3 of n - Overload Resolution

提交回复
热议问题