__FILE__宏操作在编译时处理 [英] __FILE__ macro manipulation handling at compile time

查看:140
本文介绍了__FILE__宏操作在编译时处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个我曾在从Solaris到Linux移植一些东西的问题是,在Solaris编译程序扩展宏 __ preprocessing到文件名中FILE __ (如MYFILE.CPP),而GCC在Linux上expandeds出来的完整路径(如/home/user/MyFile.cpp)。这可以通过基名(),但合理地轻松解决....如果你使用了很多,然后基名,所有那些()的调用已经得到补充,对吧?

One of the issues I have had in porting some stuff from Solaris to Linux is that the Solaris compiler expands the macro __FILE__ during preprocessing to the file name (e.g. MyFile.cpp) whereas gcc on Linux expandeds out to the full path (e.g. /home/user/MyFile.cpp). This can be reasonably easily resolved using basename() but....if you're using it a lot, then all those calls to basename() have got to add up, right?

这里的问题。有没有使用模板和静态元编程,运行基本名(),或在编译时类似的方法吗?由于 __ __ FILE 是恒定的,在编译时已知这可能更容易。你有什么感想?能不能做到?

Here's the question. Is there a way using templates and static metaprogramming, to run basename() or similar at compile time? Since __FILE__ is constant and known at compile time this might make it easier. What do you think? Can it be done?

推荐答案

目前还没有在编译时做满弦的处理方式(最大的,我们可以在模板怪异的四字符文字工作)。

There is currently no way of doing full string processing at compile time (the maximum we can work with in templates are the weird four-character-literals).

为什么不直接保存处理后的名称静态的,例如:

Why not simply save the processed name statically, e.g.:

namespace 
{
  const std::string& thisFile() 
  {
      static const std::string s(prepocessFileName(__FILE__));
      return s;
  }
}

这样,你只是做每个文件一旦工作。当然,你也可以包装成宏等这一点。

This way you are only doing the work once per file. Of course you can also wrap this into a macro etc.

这篇关于__FILE__宏操作在编译时处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆