C ++ generic programming - Extended

Do the type parameter is the main form of C ++ template implementation. Thus achieving a class template -> template class -> instance of process

In addition of course can also refer to the implementation, parameter determines the type of approach the bitset.

#include <iostream>usingnamespace std;
template <bool condition>
void Func()
{if(condition)
        cout<<"trueFunc"<<endl;
    else
        cout<<"falseFunc"<<endl;
}//template <uint32_t bitnu>
template <uint32_t bitnu = 64> //可以有默认参数class MyBitSet
{public:
    MyBitSet():pbits ( carnew

 

    



 [bitnu]){}
    ~MyBitSet()
    {
        if(pbits)
            delete pbits;
    }
    void dis()
    {
        cout<<"it's a "<<bitnu<<" bit BitClass"<<endl;
    }
    char& operator[](uint32_t offset)
    {
        return pbits[offset];
    }
private:
    char *pbits;
};


int main(int argc, char *argv[])
{
    Func<true>();
    Func<false>();

    MyBitSet<32>bit;
    bit.dis();
    bit[0]= 'a';
    bit[2]= 'g';
    cout<<"bit[0]:"<<bit[0]<<"--"<<"bit[2]"<<bit[2]<<endl;
    MyBitSet<> bit64;
    bit64.dis();
    bit[53]=0x01;
    cout<<"bit[53]:"<<int(bit[53])<<endl;
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/wangkeqin/p/11767418.html