ven*_*rty 23 c++ templates compile-time
template<unsigned int n>
struct Factorial {
enum { value = n * Factorial<n-1>::value};
};
template<>
struct Factorial<0> {
enum {value = 1};
};
int main() {
std::cout << Factorial<5>::value;
std::cout << Factorial<10>::value;
}
Run Code Online (Sandbox Code Playgroud)
上面的程序在编译期间计算阶乘值.我想在编译时打印阶乘值,而不是在运行时使用cout打印.我们怎样才能在编译时打印阶乘值?
我正在使用VS2009.
谢谢!
Naw*_*waz 33
factorial可以在编译器生成的消息中打印为:
template<int x> struct _;
int main() {
_<Factorial<10>::value> __;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
错误信息:
prog.cpp:14:32:错误:聚合'_ <3628800> __'的类型不完整,无法定义_ :: value> __; ^
这3628800
是阶乘的10
.
请在ideone上查看:http://ideone.com/094SJz
所以你在寻找这个吗?
编辑:
Matthieu要求一个聪明的技巧来打印阶乘并让编译继续.这是一次尝试.它没有给出任何错误,因此编译成功时只有一个警告.
template<int factorial>
struct _{ operator char() { return factorial + 256; } }; //always overflow
int main() {
char(_<Factorial<5>::value>());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
它使用此警告进行编译:
main.cpp:在'_ :: operator char()[with int factorial = 120]'的实例化中:main.cpp:16:39:从这里需要main.cpp:13:48:warning:隐式常量转换溢出[-Woverflow] struct _ {operator char(){return factorial + 256; }}; //总是溢出
这120
是阶乘的5
.
在ideone演示:http://coliru.stacked-crooked.com/a/c4d703a670060545
您可以编写一个漂亮的宏,并将其用作:
#define PRINT_AS_WARNING(constant) char(_<constant>())
int main()
{
PRINT_AS_WARNING(Factorial<5>::value);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
那看起来很棒.
我正在学习TMP的基础知识,并希望在编译时知道结果以确保逻辑正确.
在这种情况下,你真正想要的是静态断言:
static_assert(Factorial<5> ::value == 120, "5! should be 120");
static_assert(Factorial<10>::value == 3628800, "10! should be 3628800");
Run Code Online (Sandbox Code Playgroud)
如果你的编译器还不支持static_assert
,你可以使用BOOST_STATIC_ASSERT
.
归档时间: |
|
查看次数: |
7804 次 |
最近记录: |