Of course. This is a classic and excellent C++ problem: mapping a runtime value to a compile-time template argument. The core issue remains that you cannot pass a runtime value directly, but we can build a dispatcher that hides the if/switch logic from the call site, giving you the clean, single-line call you want.
Since your value is now an enum, the best solution is a compile-time function pointer table. It is extremely efficient (a single array lookup and an indirect function call at runtime) and completely avoids any if/else or switch statements in the source code of the dispatcher.
Setup for the Examples
Let's assume this setup:
#include <iostream>
#include <array>
#include <utility> // For std::index_sequence
// The enum that exists at runtime
enum class MyEnum {
Value1,
Value2,
Value3
// Add other values here
};
// The template 'T' that takes an enum value
template<MyEnum E>
struct T {
static constexpr MyEnum value = E;
};
// The function we want to call with different specializations
template<typename T_Instance>
void funcaboba();
// Specializations of funcaboba for each enum value
template<> inline void funcaboba<T<MyEnum::Value1>>() { std::cout << "Called funcaboba for Value1\n"; }
template<> inline void funcaboba<T<MyEnum::Value2>>() { std::cout << "Called funcaboba for Value2\n"; }
template<> inline void funcaboba<T<MyEnum::Value3>>() { std::cout << "Called funcaboba for Value3\n"; }
// The object containing the runtime enum
struct SomeType {
MyEnum selfType;
};
Solution: The Function Pointer Dispatch Table (Recommended)
This is the cleanest and most efficient solution. We create a constexpr array of function pointers at compile time. At runtime, we use the integer value of your enum to index into this array and call the correct function.
// This helper generates the dispatch table at compile time.
// It uses a lambda and a parameter pack to avoid writing out every entry.
template<template<MyEnum> class T_template, MyEnum... Es>
constexpr auto make_dispatch_table(std::integer_sequence<MyEnum, Es...>) {
// The type of our function pointer
using FuncPtr = void(*)();
// Create the array of function pointers at compile time
return std::array<FuncPtr, sizeof...(Es)>{
&funcaboba<T_template<Es>>...
};
}
// The inline dispatcher function you will call
template<template<MyEnum> class T_template>
inline void dispatch_funcaboba(MyEnum runtime_enum) {
// This is the sequence of all your enum values.
// IMPORTANT: Make sure this list is complete and in order from 0 upwards.
constexpr auto relevant_enums = std::integer_sequence<MyEnum,
MyEnum::Value1,
MyEnum::Value2,
MyEnum::Value3
>{};
// 1. The table is created once at compile time.
static constexpr auto dispatch_table = make_dispatch_table<T_template>(relevant_enums);
// 2. At runtime, we just do an array lookup.
// The cast to size_t turns the enum value into an index (0, 1, 2...).
dispatch_table[static_cast<size_t>(runtime_enum)]();
}
How to Use It
Now, your calling code becomes a beautiful one-liner, completely hiding the dispatch mechanism.
void your_calling_code(SomeType* typedPtr) {
// Instead of:
// if (typedPtr->selfType == MyEnum::Value1) { ... } else { ... }
// You write this single, clean line:
dispatch_funcaboba<T>(typedPtr->selfType);
}
int main() {
SomeType obj1{MyEnum::Value1};
SomeType obj2{MyEnum::Value2};
SomeType obj3{MyEnum::Value3};
std::cout << "Dispatching for obj1: ";
your_calling_code(&obj1); // Output: Called funcaboba for Value1
std::cout << "Dispatching for obj2: ";
your_calling_code(&obj2); // Output: Called funcaboba for Value2
std::cout << "Dispatching for obj3: ";
your_calling_code(&obj3); // Output: Called funcaboba for Value3
}
Why This is the Best Solution
- No
if/switch in Source: The dispatcher function dispatch_funcaboba contains no conditional branching statements in its source code. The logic is encoded in the data structure (std::array).
- Highly Efficient: At runtime, this compiles down to:
- Reading the enum value.
- An array lookup (base address + index * size).
- An indirect function call.
This is typically faster than a series of
if/else if checks, especially as the number of enum values grows.
- Compile-Time Safety: The
dispatch_table is built and validated entirely at compile time. If you forget to implement a funcaboba specialization for an enum in your integer_sequence, you will get a compile-time error, not a runtime crash.
- Extensible: To add a new enum value
MyEnum::Value4, you simply:
- Add
MyEnum::Value4 to the enum class definition.
- Add it to the
integer_sequence in the dispatcher.
- Provide the
template<> void funcaboba<T<MyEnum::Value4>>() specialization.
The dispatcher code itself does not need to change.