Special-Case Compilation of C + + templates to multiple definition issues

Source: Internet
Author: User
Tags strcmp

Before I met some questions about the template special case, I summarize.

Template exceptions are a feature of the new C + + standard that allows you to customize the implementation of certain templates, such as the comparison function compare can use the Less<t> Standard Library template to compare types such as String, int, char, pointer, and so on, but if there is a const char* Type and compare the dictionary size of a string, it is different from the previous comparison:

#ifndef  A_H#define A_H#include <iostream> #include  <cstring>using std:: Less;template <typename t>int compare (CONST&NBSP;T&NBSP;V1,CONST&NBSP;T&NBSP;V2)  {         if  (Less<t> () (V1,V2))       {        return -1;    }     else if  (Less<t> () (V2,V1))     {         return 1;    }    else    {         return 0;    }}template <>inline  int compare (const char *const a,const char *const b)  {  &NBSP;&NBSP;&NBSP;RETURN&NBSP;STRCMP (A, b);} #endif   #include   "A.H" Using std::cout;using std::endl;int main () {&Nbsp;   cout << compare ( << endl;    )   cout << compare ("123", "ASD")  << endl;       return 0;}

Now it looks fine, and only these two files are compiled and run correctly, if you have a file containing a.h a.cpp:

#include "A.h"

There is only one sentence, but the inclusion of the A.h file means that the definition of its template function is included, and the function of the exception is similar to a normal function, then A.cpp, Main.cpp contains multiple definitions of the same function, so there is a redefinition problem when linking.

Workaround:

When using inline inline to declare special templates, the definition of some functions can be contained in multiple files (implementations of some functions may not support inline):

Template <>inline int compare (const char *a,const char *b) {return strcmp (A, b);}

The other one is to use a file containing the header file, so that all linked files have only one exception definition

The third is to define a function with the same name, calling the non-template function by overloading (at the same time as the parameter match level, the non-template overloaded function overrides the call)

 a.h#ifndef A_H#define A_H#include <iostream> #include  <cstring>using  std::less;template <typename t>int compare (const t v1,const t  V2) {    if  (less<t> () (V1,V2))     {         return -1;    }    else if  ( Less<t> () (V2,V1))     {        return 1;     }    else    {         return 0;    }}int compare (const char *a,const  CHAR&NBSP;*B)  ;// a.cpp#include  "A.h" Int compare (const char *a, const  CHAR&NBSP;*B) {    std::cout <<  "const char*" <<std::endl;     retURN&NBSP;STRCMP (A, b);}  main.cpp  Same

If there is any other way, please communicate with us.

This article is from the "zmh009" blog, make sure to keep this source http://zmh009.blog.51cto.com/11619347/1883801

Special-Case Compilation of C + + templates to multiple definition issues

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.