Created
July 2, 2013 10:55
-
-
Save mejedi/5908409 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cstddef> | |
template<size_t n> | |
struct cn { | |
char data[n+1]; | |
}; | |
template<typename id, size_t index, size_t val> | |
cn<val> magic(id, cn<index>, cn<val>); | |
#define counter_read(id) \ | |
(sizeof( \ | |
magic(id(), cn<1>(), \ | |
magic(id(), cn<2>(), \ | |
magic(id(), cn<4>(), \ | |
magic(id(), cn<8>(), \ | |
magic(id(), cn<16>(), \ | |
magic(id(), cn<32>(), \ | |
magic(id(), cn<64>(), \ | |
magic(id(), cn<128>(), cn<0>())))))))))-1) | |
#define counter_inc(id) \ | |
cn<counter_read(id)+1> magic(id, \ | |
cn<(counter_read(id)+1)&~counter_read(id)>, \ | |
cn<(counter_read(id)+1)&counter_read(id)>) | |
struct my_cnt {}; | |
size_t const a = counter_read( my_cnt ); | |
counter_inc( my_cnt ); | |
counter_inc( my_cnt ); | |
counter_inc( my_cnt ); | |
counter_inc( my_cnt ); | |
counter_inc( my_cnt ); | |
size_t const b = counter_read( my_cnt ); | |
counter_inc( my_cnt ); | |
#include <iostream> | |
int main() { | |
std::cout << a << ' ' << b << '\n'; | |
std::cout << counter_read( my_cnt ) << '\n'; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment