如何在发布模式下自动注释行?

发布于 2024-08-10 00:22:15 字数 374 浏览 9 评论 0原文

我需要仅在调试模式下“活动”一些代码行,并在发布模式下忽略它们。 有没有办法做这样的事情:

#include <iostream>
using namespace std;

#ifdef _TEST_
#define _cerr cerr
#else
#define _cerr // cerr
#endif

int main() {
   _cerr << "TEST message" << endl;
}

这样,当未定义 _TEST_ 时,某些行会被注释或从代码中删除。我知道注释是在其余部分之前处理的,所以这段代码是错误的。但是,如何在不显式使用 #ifdefs 的情况下获得我需要的行为呢?

I need to have some lines of code "active" in debug mode only, and ignored in release mode.
Is there a way to do something like this:

#include <iostream>
using namespace std;

#ifdef _TEST_
#define _cerr cerr
#else
#define _cerr // cerr
#endif

int main() {
   _cerr << "TEST message" << endl;
}

So that when _TEST_ is not defined, some lines are commented, or removed from the code. I know that comments are processed before the rest, so this code is wrong. But how can I get the behaviour I need without using #ifdefs explicitely?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

夜清冷一曲。 2024-08-17 00:22:16

如果您想要的是在发布版本中删除的调试日志记录,您可以执行以下操作:

#ifdef _TEST_
#define LOG(x) (cerr << x << endl)
#else
#define LOG(x)
#endif

...

int main() {
    LOG("TEST message");
}

If what you are after is debug logging which is removed in release builds, you can do something like:

#ifdef _TEST_
#define LOG(x) (cerr << x << endl)
#else
#define LOG(x)
#endif

...

int main() {
    LOG("TEST message");
}
殊姿 2024-08-17 00:22:16

使用这个:

#ifdef _TEST_
#define DEBUG_TEST(x) x
#else
#define DEBUG_TEST(x)
#endif

int main() {
    DEBUG_TEST(_cerr << "TEST message" << endl);
}

Use this:

#ifdef _TEST_
#define DEBUG_TEST(x) x
#else
#define DEBUG_TEST(x)
#endif

int main() {
    DEBUG_TEST(_cerr << "TEST message" << endl);
}
黑寡妇 2024-08-17 00:22:16

ifdefs 是可行的方法。您还如何知道编译器是否处于发布模式与调试模式?除了在预处理阶段之外,还有什么时候会进行传达?您可以在什么其他阶段决定删除/添加代码(除了在模板生成期间)。嘿,也许我们可以使用模板生成...但是您仍然必须以某种方式关闭 ifdef 来控制您的模板。

也许有一种我没有想到的非常巧妙的方法来做到这一点,但每个人都知道/使用 ifdefs 来达到这个目的。如果你给他们扔一个曲线球,只会大大增加维护代码的人力成本。

坚持使用 ifdef。

ifdefs are the way to go. How else would you know if the compiler is in release vs debug mode? When else besides during the preprocessing stage would that be communicated? At what other stage could you decide to remove/add code (besides during template generation). Hey maybe we can use template generation... but you still have to key off the ifdef somehow to control your template.

Maybe there's a really slick way to do this that I'm not thinking of, but everyone knows/uses ifdefs for this purpose. If you throw them a curveball its only going to drastically increase the human cost of maintaining your code.

Stick with ifdefs.

难得心□动 2024-08-17 00:22:16

这里基本上满足了您的要求:

#ifdef _TEST_
#define _cerr  cerr
#else
#define _cerr  if (1) {} else cerr
#endif

但是,如果您收到编译器关于不明确的 else 的警告,如果您编写如下内容,请不要感到惊讶:

if (something)
  _cerr << "Why?" << std::endl;

您应该始终意识到这一事实_cerr 实际上是一个不平凡的宏。

This here basically does what you are asking for:

#ifdef _TEST_
#define _cerr  cerr
#else
#define _cerr  if (1) {} else cerr
#endif

But don't be surprised if you for example get compiler warnings about ambiguous else if you write something like this:

if (something)
  _cerr << "Why?" << std::endl;

You should always be aware of the fact that this _cerr is in fact a non-trivial macro.

魔法少女 2024-08-17 00:22:16

将 _cerr 定义为空将使编译失败。您可以改为定义一个在发布模式下排除的宏。

例如:

#ifdef _TEST_
#define LOG_ERROR(log) cerr << log << endl;
#else
#define LOG_ERROR(log) 
#endif

然后在您的代码中:

int main() {
   LOG_ERROR("TEST message");
}

Defining _cerr to nothing will fail the compilation. You could instead define a macro that you exclude while in release mode.

For example:

#ifdef _TEST_
#define LOG_ERROR(log) cerr << log << endl;
#else
#define LOG_ERROR(log) 
#endif

Then in your code:

int main() {
   LOG_ERROR("TEST message");
}
筱武穆 2024-08-17 00:22:16
int main() {
#ifdef _TEST_
   _cerr << "TEST message" << endl;
#endif
}
int main() {
#ifdef _TEST_
   _cerr << "TEST message" << endl;
#endif
}
枫林﹌晚霞¤ 2024-08-17 00:22:16

不,绝对不。

尝试对此进行变体:(

#ifdef _TEST_
    ostream& _cerr = cerr;
#else
    ostringstream _cerr;
#endif

基本上,您需要一个仅丢弃其输入的流。)

No. Absolutely, no.

Try a variation on this:

#ifdef _TEST_
    ostream& _cerr = cerr;
#else
    ostringstream _cerr;
#endif

(Basically you would want a stream which just discards its input.)

倾城花音 2024-08-17 00:22:16

对于“没有登录发布”的更好的解决方案是以下类:

class NullStream {
   template<typename T> NullStream& operator<< const(T&) { }
};

使用:

#ifdef DEBUG
#define CERR std::cerr
#else
#define CERR NullStream()
#endif

A nicer solution for the "no logging in release" is the following class:

class NullStream {
   template<typename T> NullStream& operator<< const(T&) { }
};

Use:

#ifdef DEBUG
#define CERR std::cerr
#else
#define CERR NullStream()
#endif
掀纱窥君容 2024-08-17 00:22:16

创建您自己的 NULL 流。

#include <iostream>

class NullStream    {};
template<typename T>
NullStream& operator <<(NullStream& n,T const& data)                        {return n;}
NullStream& operator <<(NullStream& n,std::ostream& (*)(std::ostream&))     {return n;}

#ifdef  _TEST_
#define myerr       std::cerr
#else
NullStream  myerrstream;
#define myerr       myerrstream
#endif

int main()
{
    myerr << "Hi" << std::endl;;
    myerr << std::endl;;
}

Create your own NULL stream.

#include <iostream>

class NullStream    {};
template<typename T>
NullStream& operator <<(NullStream& n,T const& data)                        {return n;}
NullStream& operator <<(NullStream& n,std::ostream& (*)(std::ostream&))     {return n;}

#ifdef  _TEST_
#define myerr       std::cerr
#else
NullStream  myerrstream;
#define myerr       myerrstream
#endif

int main()
{
    myerr << "Hi" << std::endl;;
    myerr << std::endl;;
}
青柠芒果 2024-08-17 00:22:15

您可以为此使用宏:

#ifdef _TEST_
#define DEBUG_ONLY(x) x;
#else
#define DEBUG_ONLY(x)
#endif

int main() {
    DEBUG_ONLY(cerr << "TEST message" << endl)
}

You can use a macro for this:

#ifdef _TEST_
#define DEBUG_ONLY(x) x;
#else
#define DEBUG_ONLY(x)
#endif

int main() {
    DEBUG_ONLY(cerr << "TEST message" << endl)
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文