From Josuttis: Do different template functions, that instantiate to the same function signature given particular types, result in ODR invalidity?

后端 未结 1 2020
广开言路
广开言路 2021-02-02 17:28

In Josuttis\' and Vandevoorde\'s well-known book on templates, C++ Templates: The Complete Guide, they discuss details regarding the overloading of function templates.

I

相关标签:
1条回答
  • 2021-02-02 18:09

    Apologies for the earlier, incorrect answer. The example does indeed seem to be correct, and there is actually a similar example in the standard itself (C++11, 14.5.6.1/1-2). Let me just quote it in its entirety:

    1. It is possible to overload function templates so that two different function template specializations have the same type. [ Example:

      // file1.c
      
      template<class T> void f(T*);
      
      void g(int* p) {
          f(p); // calls f<int>(int*)
      }
      
      
      // file2.c
      
      template<class T> void f(T);
      
      void h(int* p) {
          f(p); // calls f<int*>(int*)
      }
      

      end example ]

    2. Such specializations are distinct functions and do not violate the one definition rule (3.2).

    In your case, you have two different function templates, both called f1 (which is fine because you can overload function templates), and they happen to have specializations which have the same type.

    0 讨论(0)
提交回复
热议问题