gpt4 book ai didi

something went wrong when I tried to apply #define in a function [closed](尝试在函数中应用#DEFINE时出错[已关闭])

转载 作者:bug小助手 更新时间:2023-10-25 22:35:18 27 4
gpt4 key购买 nike




I tried to apply #define in a conditional statement, however, it seems that something went wrong.

我试图在条件语句中应用#DEFINE,但是,似乎出了问题。


// The mode is forward by default
#define FOR

if ( mode == "forward")
{
clog << "mode == forward" << endl;
}

else if (mode == "reverse")
{
clog << "Enter reverse" << endl;

#define REV
#undef FOR
}
else
{
cerr << "NOT A VALID MODE (FORWARD OR REVERSE)" << endl;
}

#ifdef FOR
cout << "Switch to forward mode." << endl;
#endif

#ifdef REV
cout << "Switch to reverse mode." << endl;
#endif

and the output is

输出结果是


mode == forward
Switch to reverse mode

Isn't this contradictory?

这不是自相矛盾吗?


I think it may be the problem of compiling.
After precompiling, the code looks like this

我认为这可能是汇编的问题。预编译后,代码如下所示


int main()
{
string mode = "forward";
if ( mode == "forward")
{
clog << "mode == forward" << endl;
}

else if (mode == "reverse")
{
clog << "Enter reverse" << endl;
}
else
{
cerr << "NOT A VALID MODE (FORWARD OR REVERSE)" << endl;
}

cout << "Switch to reverse mode." << endl;
}

I'm just new to C++, can anyone tell me why this could happen?

我刚接触C++,有人能告诉我为什么会发生这种情况吗?


更多回答

That define REV knows absolutely nothing about the if-statement it's inside. The preprocessor knows nothing about the language and its control flow, only bits of raw text and how to replace them.

定义Rev对它所在的if-语句一无所知。预处理器对语言及其控制流一无所知,只知道一些原始文本以及如何替换它们。

What exactly are you trying to achieve here? Preprocessor macros are not the right tool for it.

你在这里到底想要达到什么目的?预处理器宏不是适合它的工具。

Seems like you need to learn the difference between compile-time and run-time, and to understand what preprocessor directives do.

您似乎需要了解编译时和运行时之间的区别,并了解预处理器指令的作用。

@alter_igel thankyou!!! I get it.

@ALTER_IGEL谢谢您!我懂了。

Indentation is a powerful force of deception.

缩进是一种强大的欺骗力量。

优秀答案推荐

I think you should have been clearer in stating WHAT you want.
Using #define to alter the behavior of your code can be done but ususally only needed if you really want to enable/disable code at compile time (e.g. for release/debug, gcc/clang/MSVC compiler things). And even that should be reduced to a minimum.

我认为你应该更清楚地说明你想要什么。可以使用#Define来改变代码的行为,但通常只有当你真的想要在编译时启用/禁用代码时(例如,对于发布/调试、GCC/Clang/msvc编译器之类的事情),才需要这样做。即使是这样,也应该减少到最低限度。


In C++ try to stay as typesafe as you can and the preprocessor is usually not that either. If you have to make decissions at compile time then you can also use function templates.

在C++中,尽量保持类型安全,而预处理器通常也不是这样。如果您必须在编译时进行判定,那么您也可以使用函数模板。


#include <iostream>

// if you got options make an enum
// this will even prevent you from
// being able selecting wrong modes ever
// #define will NEVER give you that guarantee
enum class modes
{
reverse,
forward
};

template<modes mode>
void select_at_compile_time()
{
if constexpr (mode == modes::reverse)
{
std::cout << "compile time reverse\n";
}
else
{
std::cout << "compile time forward\n";
}
}

void select_mode_at_runtime(modes mode)
{
if (mode == modes::reverse)
{
std::cout << "runtime reverse\n";
}
else
{
std::cout << "runtime forward\n";
}
}


int main()
{
select_at_compile_time<modes::forward>();
select_mode_at_runtime(modes::reverse);
return 0;
}

更多回答

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com