#include <iostream>
template <unsigned SIZE>
struct offset_size {
typedef typename offset_size<SIZE - 1>::type type;
};
template <>
struct offset_size<0> {
typedef unsigned char type;
};
template <>
struct offset_size<257> {
typedef unsigned int type;
};
template<unsigned SIZE>
class circular_buffer {
public: // Added this line so we can inspect the sizes from main()
unsigned char buffer[SIZE];
typename offset_size<SIZE>::type head; // index
typename offset_size<SIZE>::type tail; // index
};
int main() {
std::cout << sizeof(circular_buffer<256>().head) << std::endl;
std::cout << sizeof(circular_buffer<257>().head) << std::endl;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgoKICAgIHRlbXBsYXRlIDx1bnNpZ25lZCBTSVpFPgogICAgc3RydWN0IG9mZnNldF9zaXplIHsKICAgICAgICB0eXBlZGVmIHR5cGVuYW1lIG9mZnNldF9zaXplPFNJWkUgLSAxPjo6dHlwZSB0eXBlOwogICAgfTsKCiAgICB0ZW1wbGF0ZSA8PgogICAgc3RydWN0IG9mZnNldF9zaXplPDA+IHsKICAgICAgICB0eXBlZGVmIHVuc2lnbmVkIGNoYXIgdHlwZTsKICAgIH07CgogICAgdGVtcGxhdGUgPD4KICAgIHN0cnVjdCBvZmZzZXRfc2l6ZTwyNTc+IHsKICAgICAgICB0eXBlZGVmIHVuc2lnbmVkIGludCB0eXBlOwogICAgfTsKCiAgICB0ZW1wbGF0ZTx1bnNpZ25lZCBTSVpFPgogICAgY2xhc3MgY2lyY3VsYXJfYnVmZmVyIHsKICAgIHB1YmxpYzogLy8gQWRkZWQgdGhpcyBsaW5lIHNvIHdlIGNhbiBpbnNwZWN0IHRoZSBzaXplcyBmcm9tIG1haW4oKQogICAgICAgdW5zaWduZWQgY2hhciBidWZmZXJbU0laRV07CiAgICAgICB0eXBlbmFtZSBvZmZzZXRfc2l6ZTxTSVpFPjo6dHlwZSBoZWFkOyAvLyBpbmRleAogICAgICAgdHlwZW5hbWUgb2Zmc2V0X3NpemU8U0laRT46OnR5cGUgdGFpbDsgLy8gaW5kZXgKICAgIH07CiAgICAKaW50IG1haW4oKSB7CiAgICBzdGQ6OmNvdXQgPDwgc2l6ZW9mKGNpcmN1bGFyX2J1ZmZlcjwyNTY+KCkuaGVhZCkgPDwgc3RkOjplbmRsOwogICAgc3RkOjpjb3V0IDw8IHNpemVvZihjaXJjdWxhcl9idWZmZXI8MjU3PigpLmhlYWQpIDw8IHN0ZDo6ZW5kbDsKfQ==