The following is the sample code, which cannot compile.
We use iteration function to iterate over some range and run the lambda callback function. The iterate function will pass some indications (i.e. type) to the callback lambda function, which will then do works based on indications. Since these indications are fixed in compile-time, I believe there is a way to remove all indication overhead in runtime. But how..?
template<typename FuncT>
void iterate(const FuncT& func) {
for(int i = 0; i < 5; ++i)
func(0, i);
for(int i = 0; i < 5; ++i)
func(1, i);
}
template<int d> int getValue(int i) { return d+i; }
// this has run-time overhead compared to the template version
// int getValue(int d, int i) {return d+i; }
int main() {
iterate([&](const int type, int index){
// error: cannot compiler here
// candidate template ignored: invalid explicitly-specified argument for template parameter 'd'
std::cout<<getValue<type>(index)<<std::endl;
});
}