Skip to content

Instantly share code, notes, and snippets.

@blubberdiblub
Last active September 1, 2019 02:59
Show Gist options
  • Save blubberdiblub/28b20c85b0af94888cdc99d22e1de488 to your computer and use it in GitHub Desktop.
Save blubberdiblub/28b20c85b0af94888cdc99d22e1de488 to your computer and use it in GitHub Desktop.
Integral promotion and usual arithmetic conversion
#include <cstdint>
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <sstream>
#include <string>
#include <typeinfo>
#include <cxxabi.h>
using namespace std;
template <typename T>
string annotate(T value) {
ostringstream buffer;
int status;
auto type_name = abi::__cxa_demangle(typeid(value).name(), 0, 0, &status);
buffer << type_name << '(' << +value << ')';
free(type_name);
return buffer.str();
}
#define STR_HELPER(x) #x
#define STR(x) STR_HELPER(x)
#define DO_OP(a, op, b) \
do { \
auto a ## _op_ ## b = a op b; \
cout << setw(40) << annotate(a) << " " STR(op) " " << setw(40) << annotate(b) << " = " << annotate(a ## _op_ ## b) << endl; \
} while(0)
#define DO_10(a, op) \
do { \
cout << endl; \
DO_OP(a, op, s_c2); \
DO_OP(a, op, u_c2); \
DO_OP(a, op, __s2); \
DO_OP(a, op, u_s2); \
DO_OP(a, op, __i2); \
DO_OP(a, op, u_i2); \
DO_OP(a, op, __l2); \
DO_OP(a, op, u_l2); \
DO_OP(a, op, _ll2); \
DO_OP(a, op, ull2); \
} while(0)
#define DO_100(op) \
do { \
DO_10(s_c1, op); \
DO_10(u_c1, op); \
DO_10(__s1, op); \
DO_10(u_s1, op); \
DO_10(__i1, op); \
DO_10(u_i1, op); \
DO_10(__l1, op); \
DO_10(u_l1, op); \
DO_10(_ll1, op); \
DO_10(ull1, op); \
} while(0)
int main() {
signed char s_c1 = -42;
unsigned char u_c1 = -42;
short __s1 = -42;
unsigned short u_s1 = -42;
int __i1 = -42;
unsigned int u_i1 = -42;
long __l1 = -42;
unsigned long u_l1 = -42;
long long _ll1 = -42;
unsigned long long ull1 = -42;
signed char s_c2 = 23;
unsigned char u_c2 = 23;
short __s2 = 23;
unsigned short u_s2 = 23;
int __i2 = 23;
unsigned int u_i2 = 23;
long __l2 = 23;
unsigned long u_l2 = 23;
long long _ll2 = 23;
unsigned long long ull2 = 23;
DO_100(+);
DO_100(-);
DO_100(*);
DO_100(/);
DO_100(%);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment