【发布时间】:2014-09-03 15:25:55
【问题描述】:
如果我有一个功能
int calcStuff_dynamic(const int a, const int b)
还有一些模板元代码
template<int a, int b>
struct calcStuff_static {
static const int value = //some more code
};
有没有办法写一个包装器
int calcStuff(const int a, const int b) {
IF_THESE_ARE_KNOWN_CONSTANTS_AT_COMPILE_TIME(a, b)
return calcStuff_static<a, b>::value;
ELSE_TEMPLATE_WOULD_FAIL
return calcStuff_dynamic(a, b);
}
【问题讨论】:
-
“IF_THESE_ARE_KNOWN”是什么意思?
-
您在静态情况下使用的算法是否与在非静态情况下不同?为什么?否则,您正在寻找
constexpr。 -
a和calcStuff中的b不是常量表达式,因此它们不能用作calcStuff_dynamic中的模板参数。 -
gcc 4.3 太旧了,当你给它无意义的编译标志时,它甚至不会出错:-)
标签: c++ templates template-meta-programming compile-time compile-time-constant