I am testing combinations of various optimizations and for these I need a static-if as described in http://channel9.msdn.com/Events/GoingNative/GoingNative-2012/Static-If-I-Had-a-Hammer to enable and disable specific optimizations. if(const-expr) does not always work as some optimizations involve changing the data layout and this can not be done at function scope.
Basically what I want is this:
template<bool enable_optimization>
class Algo{
struct Foo{
int a;
if(enable_optimization){
int b;
}
void bar(){
if(enable_optimization){
b = 0;
}
}
};
};
(Yes, the smaller memory footprint of removing b from the data layout is relevant in my case.)
Currently I am faking it using a very bad hack. I am looking for a better way of doing this.
File a.h
#ifndef A_H
#define A_H
template<class enable_optimization>
class Algo;
#include "b.h"
#endif
File b.h (this file is autogenerated from a Python script)
#define ENABLE_OPTIMIZATION 0
#include "c.h"
#undef
#define ENABLE_OPTIMIZATION 1
#include "c.h"
#undef
File c.h
template<>
class Algo<ENABLE_OPTIMIZATION>{
struct Foo{
int a;
#if ENABLE_OPTIMIZATION
int b;
#endif
void bar(){
#if ENABLE_OPTIMIZATION
b = 0;
#endif
}
};
};
Does anyone know of a better way of doing this? Theoretically it can be done using template meta programming and at first I used it. At least the way I used it was a pain in the ass and lead to completely unreadable and bloated code. Using the hack above resulted in a significant productivity boost.
EDIT: I have several optimization flags and these interact.