October 22, 2024
Chicago 12, Melborne City, USA
templates

Specialize function by argument values known at compile time in C++


I have some function with 2 arguments: distance and radius

float expensive_function(float distance, float radius) {
    return distance + radius;
}

The whole project only uses 2 different radiuses and they both are known at compile time (1.2f and 3.4f). Is there a way to specialize this function knowing that? I would like something like that, but it doesn’t compile:

template <float RADIUS>
float expensive_function(float distance) {
    return distance + RADIUS;
}

expensive_function<1.2f>(run_time_distance);
expensive_function<3.4f>(run_time_distance);

Compiler says:
Candidate template ignored: invalid explicitly-specified argument for template parameter 'RADIUS'
and
No matching function for call to 'expensive_function'

I’m not very familiar with C++ template programming, so I didn’t expect it to work. I don’t know if what I’m looking for even possible. I just want to make sure compiler will generate the most performant code for either cases, plus it’s better semantically in my opinion.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video