亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

模板<unsigned int N>是什么意思?

模板<unsigned int N>是什么意思?

C++
侃侃爾雅 2019-12-09 15:27:22
在聲明模板時,我習慣于使用這種代碼:template <class T>但是在這個問題上,他們使用了:template <unsigned int N>我檢查它是否可以編譯。但是這是什么意思?它是非類型參數嗎?如果是這樣,我們如何有一個沒有任何類型參數的模板?
查看完整描述

3 回答

?
手掌心

TA貢獻1942條經驗 獲得超3個贊

是的,它是非類型參數。您可以有幾種模板參數


類型參數。

種類

模板(僅類和別名模板,無函數或變量模板)

非類型參數

指針

參考文獻

整數常量表達式

您所擁有的是最后一種。它是一個編譯時常量(所謂的常量表達式),類型為整數或枚舉。在標準中查找之后,我不得不將類模板移到“類型”部分中-即使模板不是類型。但是出于描述這些種類的目的,它們被稱為類型參數。您可以擁有具有外部鏈接的對象/函數的指針(以及成員指針)和引用(可以從其他對象文件鏈接到這些對象/函數,并且其地址在整個程序中是唯一的)。例子:


模板類型參數:


template<typename T>

struct Container {

    T t;

};


// pass type "long" as argument.

Container<long> test;

模板整數參數:


template<unsigned int S>

struct Vector {

    unsigned char bytes[S];

};


// pass 3 as argument.

Vector<3> test;

模板指針參數(將指針傳遞給函數)


template<void (*F)()>

struct FunctionWrapper {

    static void call_it() { F(); }

};


// pass address of function do_it as argument.

void do_it() { }

FunctionWrapper<&do_it> test;

模板參考參數(傳遞整數)


template<int &A>

struct SillyExample {

    static void do_it() { A = 10; }

};


// pass flag as argument

int flag;

SillyExample<flag> test;

模板模板參數。


template<template<typename T> class AllocatePolicy>

struct Pool {

    void allocate(size_t n) {

        int *p = AllocatePolicy<int>::allocate(n);

    }

};


// pass the template "allocator" as argument. 

template<typename T>

struct allocator { static T * allocate(size_t n) { return 0; } };

Pool<allocator> test;

沒有任何參數的模板是不可能的。但是沒有任何顯式參數的模板也是可能的-它具有默認參數:


template<unsigned int SIZE = 3>

struct Vector {

    unsigned char buffer[SIZE];

};


Vector<> test;

從語法上講,template<>保留標記專用的顯式模板,而不是不帶參數的模板:


template<>

struct Vector<3> {

    // alternative definition for SIZE == 3

};


查看完整回答
反對 回復 2019-12-09
?
FFIVE

TA貢獻1797條經驗 獲得超6個贊

您基于“ unsigned int”對類進行模板化。


例:


template <unsigned int N>

class MyArray

{

    public:

    private:

        double    data[N]; // Use N as the size of the array

};


int main()

{

    MyArray<2>     a1;

    MyArray<2>     a2;


    MyArray<4>     b1;


    a1 = a2;  // OK The arrays are the same size.

    a1 = b1;  // FAIL because the size of the array is part of the

              //      template and thus the type, a1 and b1 are different types.

              //      Thus this is a COMPILE time failure.

 }


查看完整回答
反對 回復 2019-12-09
?
慕雪6442864

TA貢獻1812條經驗 獲得超5個贊

完全有可能在一個整數而不是一個類型上模板化一個類。我們可以將模板化的值分配給變量,或者以其他整數形式使用的方式對其進行操作:


unsigned int x = N;

實際上,我們可以創建在編譯時評估的算法(來自Wikipedia):


template <int N>

struct Factorial 

{

     enum { value = N * Factorial<N - 1>::value };

};


template <>

struct Factorial<0> 

{

    enum { value = 1 };

};


// Factorial<4>::value == 24

// Factorial<0>::value == 1

void foo()

{

    int x = Factorial<4>::value; // == 24

    int y = Factorial<0>::value; // == 1

}


查看完整回答
反對 回復 2019-12-09
  • 3 回答
  • 0 關注
  • 1721 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號