Category : c-preprocessor

It’s easy to store an arbitrary blob of code in a macro using after every line: #define MY_switch_over_values_case_A case ‘A’ : { i++; break; } #define MY_switch_over_values_case_B case ‘B’ : { i–; break; } #define MY_switch_over_values_default default : { i*=10; break; } #define MY_switch_over_values_cases MY_switch_over_values_case_A MY_switch_over_values_case_B #define MY_switch_over_values switch(ch) { MY_switch_over_values_cases MY_switch_over_values_default } char ch; ..

Read more

I’ve written some utility macros to make turning an enum value into a std::string: #include <map> #include <string> #define MY_map_entry_(name, value) {#value, name::value} #define MY_map_2(name, v1, v2) MY_map_entry_(name, v1), MY_map_entry_(name, v2) #define MY_map_3(name, v1, v2, v3) MY_map_entry_(name, v1), MY_map_2(name, v2, v3) This works completely as-desired: enum class rgb { red, green, blue }; static const ..

Read more

Here is the case. I want to substitue ComplexDouble type and Double type to ComplexFloat and Float type in one of my files depend on a variables’ value. An example here: Original code: Tensor& float_power_out(Tensor& result, const Tensor& base, const Tensor& exp) { auto dtype = (at::isComplexType(base.scalar_type()) || at::isComplexType(exp.scalar_type())) ? at::kComplexDouble : at::kDouble; TORCH_CHECK(result.scalar_type() == ..

Read more