#include <array>
#include <cstdint>
//
// Helpers..
//
template < unsigned > struct IntegerSize;
template <> struct IntegerSize< 1 > { typedef uint8_t Type; } ;
template <> struct IntegerSize< 2 > { typedef uint16_t Type; } ;
template <> struct IntegerSize< 4 > { typedef uint32_t Type; } ;
template <> struct IntegerSize< 8 > { typedef uint64_t Type; } ;
// Calculates the one's complement sum, but without the final bit flip.
// This allows you to call this method multiple times and flip the bits
// yourself after the last call.
template < typename T>
T OnesComplementSum( T t0, T t1)
{
typedef typename IntegerSize< 2 * sizeof ( T) > :: Type TemporaryResult;
static const T cMaxValue = T( - 1 ) ;
static const unsigned cNumBits = 8 * sizeof ( T) ;
TemporaryResult sum = t0;
sum + = t1;
while ( sum > cMaxValue)
{
sum = ( sum >> cNumBits) + ( sum & cMaxValue) ;
}
return static_cast < T> ( sum) ;
}
template < typename T, typename ...U >
T OnesComplementSum( T head, U ...tail )
{
return OnesComplementSum( head, OnesComplementSum( tail...) ) ;
}
template < unsigned N> std:: array < char , N> datum( ) { return std:: array < char , N> ( ) ; }
int main( )
{
int a;
long b;
char c;
OnesComplementSum( a, b, c) ;
}
I2luY2x1ZGUgPGFycmF5PgojaW5jbHVkZSA8Y3N0ZGludD4KCgovLwovLyBIZWxwZXJzLi4KLy8KdGVtcGxhdGU8dW5zaWduZWQ+IHN0cnVjdCBJbnRlZ2VyU2l6ZTsKdGVtcGxhdGU8PiBzdHJ1Y3QgSW50ZWdlclNpemU8MT4geyB0eXBlZGVmIHVpbnQ4X3QgVHlwZTsgfTsKdGVtcGxhdGU8PiBzdHJ1Y3QgSW50ZWdlclNpemU8Mj4geyB0eXBlZGVmIHVpbnQxNl90IFR5cGU7IH07CnRlbXBsYXRlPD4gc3RydWN0IEludGVnZXJTaXplPDQ+IHsgdHlwZWRlZiB1aW50MzJfdCBUeXBlOyB9Owp0ZW1wbGF0ZTw+IHN0cnVjdCBJbnRlZ2VyU2l6ZTw4PiB7IHR5cGVkZWYgdWludDY0X3QgVHlwZTsgfTsKCgovL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50IGE7Cglsb25nIGI7CgljaGFyIGM7CgkKICAgIAoJT25lc0NvbXBsZW1lbnRTdW0oYSwgYiwgYyk7Cn0=
compilation info
prog.cpp: In function 'T OnesComplementSum(T, U ...) [with T = char, U = {}]':
prog.cpp:38:62: instantiated from 'T OnesComplementSum(T, U ...) [with T = long int, U = {char}]'
prog.cpp:38:62: instantiated from 'T OnesComplementSum(T, U ...) [with T = int, U = {long int, char}]'
prog.cpp:51:27: instantiated from here
prog.cpp:38:62: error: no matching function for call to 'OnesComplementSum()'
prog.cpp: In function 'T OnesComplementSum(T, U ...) [with T = long int, U = {}]':
prog.cpp:38:62: instantiated from 'T OnesComplementSum(T, U ...) [with T = int, U = {long int}]'
prog.cpp:38:62: instantiated from 'T OnesComplementSum(T, U ...) [with T = int, U = {long int, char}]'
prog.cpp:51:27: instantiated from here
prog.cpp:38:62: error: no matching function for call to 'OnesComplementSum()'
stdout