Use of non-type template parameters (nontype template parameters) in C + +

Source: Internet
Author: User
Tags constant expression integer

Non-type template parameters (nontype template parameters), you can use an integral type (integral type), a pointer (pointer), or a reference (reference);

The argument (argument) of a bound untyped integer parameter (nontype integral parameter) must be a constant expression (constant expression, constexpr);

You cannot bind a normal local object or a dynamic object or a referenced untyped parameter by using a global type.

For a class template (class template), the non-type template parameters are written, see: Http://en.cppreference.com/w/cpp/language/class_template

The following example contains a common notation for template types as integers, pointers, references, function pointers;

Note that pointers and referenced arguments are global variables;

The code is as follows:

=====================================//Name:CppPrimer.cpp//author:caroline//Version:1
      
.0//Description:example, UTF-8//=====================================/*eclipse CDT, gcc 4.8.1*/  
      
#include <iostream> #include <vector> #include <cstring> using namespace std;  
    Integer template template<unsigned N, unsigned m> bool Compare (const char (&AMP;P1) [N], const char (&AMP;P2) [M]) {  
    Std::cout << "Size: << N <<" "<< M << Std::endl;  
Return strcmp (P1, p2); }//pointer template<const char* c> void Pointert (const char* str) {std::cout << C << ""  
<< str << Std::endl; }//reference Template<char (&ra) [9]> void Referencet (const char* str) {std::cout << ra <&l T  
"" << str << Std::endl; } char ca[] = "Caroline"; Initialize pointer char cr[9] = "Caroline"; Initialize the reference, Contains a trailing symbol void F (const char* c) {std::cout << C << Std::endl;}  
function pointer template<void (*F) (const char*) > void Fpointert (const char* c) {F (c); int main (void) {if (compare ("Caroline", "Wendy")) {std::cout << "Caroline is long." &L  
    t;< Std::endl;  
    else {std::cout << "Wendy is long." << Std::endl; //cannot use local variable or dynamic variable as template parameter pointert<ca> ("Wendy"); Pointer referencet<cr> ("Wendy"); Quoting Fpointert<f> ("Caroline Wendy");  
function pointer return 0; }

Output:

Size:9 6  
Caroline is long.  
Caroline Wendy  
Caroline Wendy  
Caroline Wendy

Author: csdn Blog spike_king

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/cplus/

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.