#define COUNTER_READ_CRUMB( TAG, RANK, ACC ) counter_crumb( TAG(), constant_index< RANK >(), constant_index< ACC >() )
#define COUNTER_READ( TAG ) COUNTER_READ_CRUMB( TAG, 1, COUNTER_READ_CRUMB( TAG, 2, COUNTER_READ_CRUMB( TAG, 4, COUNTER_READ_CRUMB( TAG, 8, \
COUNTER_READ_CRUMB( TAG, 16, COUNTER_READ_CRUMB( TAG, 32, COUNTER_READ_CRUMB( TAG, 64, COUNTER_READ_CRUMB( TAG, 128, 0 ) ) ) ) ) ) ) )
#define COUNTER_INC( TAG ) \
constant_index< COUNTER_READ( TAG ) + 1 > \
constexpr counter_crumb( TAG, constant_index< ( COUNTER_READ( TAG ) + 1 ) & ~ COUNTER_READ( TAG ) >, \
constant_index< ( COUNTER_READ( TAG ) + 1 ) & COUNTER_READ( TAG ) > ) { return {}; }
#define COUNTER_LINK_NAMESPACE( NS ) using NS::counter_crumb;
#include <utility>
template< std::size_t n >
struct constant_index : std::integral_constant< std::size_t, n > {};
template< typename id, std::size_t rank, std::size_t acc >
constexpr constant_index< acc > counter_crumb( id, constant_index< rank >, constant_index< acc > ) { return {}; } // found by ADL via constant_index
struct my_cnt {};
int 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 );
int 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';
}
I2RlZmluZSBDT1VOVEVSX1JFQURfQ1JVTUIoIFRBRywgUkFOSywgQUNDICkgY291bnRlcl9jcnVtYiggVEFHKCksIGNvbnN0YW50X2luZGV4PCBSQU5LID4oKSwgY29uc3RhbnRfaW5kZXg8IEFDQyA+KCkgKQojZGVmaW5lIENPVU5URVJfUkVBRCggVEFHICkgQ09VTlRFUl9SRUFEX0NSVU1CKCBUQUcsIDEsIENPV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