制作函数的编译时和运行时版本的最佳方法

Best way to make both a compile-time and runtime version of a function

我有一个将被编译时和运行时函数调用的函数(gtest 和 python ctypes)。我需要一个模板化版本和一个将模板化变量作为函数参数的版本。例如

template<int A, int B, int C>
void function_compiletime(int a, int b, int c) {
    // code section 1
}

void function_runtime(int a, int b, int c, int A, int B, int C) {
    // code section 2
}

其中 // code section 1 等同于 // code section 2。我很小心,我可能会不小心更改 // code section 1 而不是 // code section 2 中的某些内容。如何强制函数体应该相同?

Best way to make both a compile-time and runtime version of a function

How can enforce that the body of the functions should be identical?

通过定义单个 constexpr 函数:

constexpr void
function_runtime(int a, int b, int c, int A, int B, int C)
{
    // code section
}