在哪一點上,模板方法的部分將由編譯器優化?請問去掉無法訪問的代碼,去掉不必要的代碼循環? (BITS使用unsigned int類型塊,整型使用無符號長的)C++模板優化
另外,有一個C++數據類型,意思是「我是你的處理器登記的大小的整」?
template<size_t bits> class IntegerFactoryImpl : public IntegerFactory<Integer<bits>>{
private:
template<int sizeOfLong, int sizeOfInt> Integer<bits> getOne(const Bits& b) const{
Integer<bits> integer = this->getOne();
size_t roof = (b.blocks() > integer.size()*(sizeOfLong/sizeOfInt))? integer.size()*(sizeOfLong/sizeOfInt) : b.blocks();
for(size_t i = 0; i < roof; ++i){
integer.at(i/(sizeOfLong/sizeOfInt)) = 0;
for(size_t j = 0; j < (sizeOfLong/sizeOfInt); ++j){
if(i % (sizeOfLong/sizeOfInt) == j){
integer.at(i/(sizeOfLong/sizeOfInt)) |= ((unsigned long)b.block(b.blocks()-i-1)) << (sizeOfInt*j);
break;
}
}
}
for(size_t i = roof; i < integer.size()*(sizeOfLong/sizeOfInt); ++i){
if(i % (sizeOfLong/sizeOfInt) == 0){
integer.at(i/(sizeOfLong/sizeOfInt)) = 0;
}
}
return integer;
}
public:
virtual ~IntegerFactoryImpl() throw(){}
virtual Integer<bits> getOne() const{
return Integer<bits>();
}
virtual Integer<bits> getOne(const Bits& b) const{
return this->getOne<sizeof(unsigned long)*8, sizeof(unsigned int)*8>(b);
}
};
會不會有這個代碼的差異(沒有模板的方法):
template<size_t bits> class IntegerFactoryImpl : public IntegerFactory<Integer<bits>>{
public:
virtual ~IntegerFactoryImpl() throw(){}
virtual Integer<bits> getOne() const{
return Integer<bits>();
}
virtual Integer<bits> getOne(const Bits& b) const{
Integer<bits> integer = this->getOne();
size_t roof = (b.blocks() > integer.size()*((sizeof(unsigned long)/sizeof(unsigned int)))? integer.size()*((sizeof(unsigned long)/sizeof(unsigned int)) : b.blocks();
for(size_t i = 0; i < roof; ++i){
integer.at(i/((sizeof(unsigned long)/sizeof(unsigned int))) = 0;
for(size_t j = 0; j < ((sizeof(unsigned long)/sizeof(unsigned int)); ++j){
if(i % ((sizeof(unsigned long)/sizeof(unsigned int)) == j){
integer.at(i/((sizeof(unsigned long)/sizeof(unsigned int))) |= ((unsigned long)b.block(b.blocks()-i-1)) << ((sizeof(unsigned int)*8)*j);
break;
}
}
}
for(size_t i = roof; i < integer.size()*((sizeof(unsigned long)/sizeof(unsigned int)); ++i){
if(i % ((sizeof(unsigned long)/sizeof(unsigned int)) == 0){
integer.at(i/((sizeof(unsigned long)/sizeof(unsigned int))) = 0;
}
}
return integer;
}
};
(編輯:我剛剛發現的代碼無法正常工作(我固定它),但原來的問題仍然適用。)
你試圖解決什麼問題,這是你的解決方案? – GManNickG 2013-02-21 21:50:54
請參閱http://stackoverflow.com/questions/582302/are-there-optimized-c-compilers-for-template-use – user1929959 2013-02-21 21:52:00
另外,是否有一個C++數據類型,意思是「我是你的大小的整數處理器註冊管理機構「。嗯「int」? – SeedmanJ 2013-02-21 21:52:30