Compiler error occurred when try to use a flexible template expression in preprocessor defines

snnn 2005-03-14 11:25:02


When I try to compile the following codes piece under Visual C++ .net 2003, I get an error “Fatal Error C1017”

typedef ::Loki::Typelist<int,::Loki::NullType> TList;
#if ::Loki::TL::Length<TList>::value
int x;
#endif

Fatal Error C1017:
invalid integer constant expression
The expression in an #if directive did not exist or did not evaluate to a constant.

::Loki::TL::Length<TList> is a struct some like this:
struct Length{
enum { value=0}; // Indeed,the value should be the length of this Typelist(TList)
}

emmm. try to the following one instead:
enum {value=::Loki::TL::Length<TList>::value};
#if value
int x;
#endif

Ok! No error, no warning. However, as you see, that’s badly! I do not need this extra enum value.

So, Help me, please! Where is the syntax error in the former case?

Thanks!
Thanks!
Thanks!
...全文
93 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jerry 2005-03-15
  • 打赏
  • 举报
回复
也许预处理器不认识C++的模板吧,但它认识ENUM常量,
我是猜的,仅供参考
snnn 2005-03-15
  • 打赏
  • 举报
回复
楼上,谢谢啦!
whoho 2005-03-15
  • 打赏
  • 举报
回复
记住,模板虽然在编译期确定,但在那之前,是不确定的
预处理只是简单的替换,以及一些简单的处理
whoho 2005-03-15
  • 打赏
  • 举报
回复
#if ::Loki::TL::Length<TList>::value //1
int x;
#endif

1处你使用了一个编译期才能获得的常数value,预处理器是不能自己计算的
^^^^^^
#if使用的要么是字面常数,要么是#define常数

value的定义要在编译时计算之后才能得到
llmsn 2005-03-15
  • 打赏
  • 举报
回复
难道不会写中文,那怎么看得出我们写的中文,回答给你又有什么用呢?

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧