是否有机会编写元编程功能,但不能扩展编译中的所有参数?只是想要一些参数作为运行时参数和一些编译。因为我知道其中一些将在1..10的范围内,但另一个未知(将在运行时知道)。编译时在C++元编程中使用运行时参数(变量)
允许使用标准的元编程例子:
unsigned int factorial(unsigned int n) {
return n == 0 ? 1 : n * factorial(n - 1);
}
template <int n>
struct factorial {
enum { value = n * factorial<n - 1>::value };
};
template <>
struct factorial<0> {
enum { value = 1 };
};
// Usage examples:
// factorial<0>::value would yield 1;
// factorial<4>::value would yield 24.
而下面是我的情况:
unsigned int cutom_imagined_function(unsigned int n, unsigned int runtime_param /* this will be given at runtime */) {
return n == 0 ? 1 : (n + runtime_param) * cutom_imagined_function(n - 1);
}
我如何转换上面元编程?并运行此假设如下(或类似的东西):
// int variable;
// variable = get_var_from_user();
// cutom_imagined_function<4>::value(variable)
你的例子没有意义。当factorial是一个只有一个参数的概念时,我该如何描述如何实现'factorial <4>(variable)'? –
@MooingDuck这只是一个例子......编辑。更好? – nosbor
@awesomeyi不能?你为什么想让编译器确定运行时间的东西? – nosbor