클래스 템플릿 특수화에서 boolalpha로 표현된 리턴값에 대해 질문이 있습니다.
해결됨
홍정모의 따라하며 배우는 C++
#pragma once #include <bitset> template <class T> class Storage8 { private: T array_[8]; public: void set(const int& index, const T& value) { array_[index] = value; } const T& get(int index) const { return array_[index]; } }; template<> class Storage8<bool> { private: unsigned char data_; public: Storage8() :data_(0){} void set(int index, bool value) { unsigned char mask = 1 << index; // left shift std::cout <<"index bit : " << std::bitset<8>(mask) << std::endl; if (value) data_ |= mask; // flag on else data_ &= ~mask; // flag off std::cout << "After masking value : " << std::bitset<8>(data_) << std::endl; } bool get(int index) { unsigned char mask = 1 << index; return(data_ & mask) != 0; // Has data_? } }; main.cpp #include <iostream> #include <array> #include "Storage8.h" using namespace std; int main() { // Define a Storage8 for integers Storage8<int> intStorage; for (int count = 0; count < 8; count++) intStorage.set(count, count); for (int count = 0; count < 8; count++) cout << intStorage.get(count) << endl; cout << "Sizeof Storage8<int> " << sizeof(Storage8<int>) << endl; // Define a Storage8 for bool Storage8<bool> boolStorage; for (int count = 0; count < 8; count++) boolStorage.set(count, count & 3); // 늘어나는 count와 3을 bitmasking for (int count = 0; count < 8; count++) cout << std::boolalpha<< boolStorage.get(count) << endl; cout << "Sizeof Storage8<bool> " << sizeof(Storage8<bool>) << endl; return 0; } 이런 결과가 나왔는데요, 원문 learncpp에서는 boolStorage.set(count, count & 3); 처럼 value값에 늘어나는 count와 숫자 3을 넣었습니다. 그런데 이 원리가 어떻게 되는지 잘 모르겠습니다.
- C++





