How C ++ templates limit Actual Application

Source: Internet
Author: User

The template application in C ++ programming language is a very important operation skill. Its Applications greatly improve the program development efficiency of programmers. In this article, we will focus on the application of C ++ template restrictions for your understanding.

1. floating point numbers cannot be used as non-type template parameters, such as: template <float/* or double */> class TT;

2. Custom classes cannot be used as template parameters. These custom classes are also non-type template parameters.

 
 
  1. // 6-14-2009  
  2. #include <iostream> 
  3. using namespace std;  
  4. // #define FLOAT  
  5. // #define TEMPLATE_OBJECT  
  6. #define COMMON_OBJECT  
  7. #ifdef FLOAT  
  8. template <float f> 
  9. class TT;  
  10. #endif  
  11. #ifdef TEMPLATE_OBJECT  
  12. template < class T > 
  13. class TM {};  
  14. template < TM<int> c > 
  15. class TT;  
  16. #endif  
  17. #ifdef COMMON_OBJECT  
  18. class TN{};  
  19. template < TN c > 
  20. class TT;  
  21. #endif 

There is also a restriction on the C ++ template, and it is very important:

The declaration and definition of a template class or template function must be in the same file! Unless the next-generation compiler supports the keyword export.

If the compiler does not support the export keyword, but we want to separate the declaration from the definition, what should we do? The method is as follows:

Write the template declaration in. h, the template definition is written in. in cpp, it should be noted that we are not in. cpp contains. h, but in main. in cpp, include these two items as follows:

 
 
  1. // test.h  
  2. template <typename T> 
  3. class Test  
  4. {  
  5. public:  
  6. void print();  
  7. };  
  8. // test.cpp  
  9. template <typename T> 
  10. void Test<T>::print()  
  11. {  
  12. cout << "Successfully!" << endl;  
  13. }  
  14. // main.cpp  
  15. #include <iostream> 
  16. using namespace std;  
  17. #include "test.h"  
  18. #include "test.cpp"  
  19. int main()  
  20. {  
  21. Test<int> t;  
  22. t.print();  
  23. return 0;  

The preceding section describes the restrictions on the C ++ template.

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.