Get the App
SLTechnology News&Howtos  ›  Development  › 

How to discuss the restriction way of C++ template

Shulou Source: shulou.com Published: 2022-06-02 22:37:29 09月13日 Update

In this issue, the editor will bring you about how to explore the restrictions on C++ templates. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

The application of templates in C++ programming language is a very important operation skill. Its application improves the programming efficiency of programmers to a great extent.

1. Floating-point numbers cannot be used as untyped template parameters such as template class TT

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

/ / 6-14-2009 # include using namespace std; / / # define FLOAT / / # define TEMPLATE_OBJECT # define COMMON_OBJECT # ifdef FLOAT template class TT; # endif # ifdef TEMPLATE_OBJECT template

< class T >

Class TM {}; template

< TM c >

Class TT; # endif # ifdef COMMON_OBJECT class TN {}; template

< TN c >

Class TT; # endif

There is another, and quite important, limitation to C++ templates:

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

What if the compiler doesn't support the export keyword, but we want to separate the declaration from the definition? The methods are as follows:

Write the template declaration in .h and the template definition in .cpp. It is important to note that we do not include .h in .cpp as usual, but in main.cpp, including these two things, such as:

/ / test.h template class Test {public: void print ();}; / / test.cpp template void Test::print () {cout

Tags: Templates templates restrictions parameters methods important keywords keywords content files types compilers analysis applications support programming compilation large professional two Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS OPPO Reno MariaDB Redmi MySQL