有没有办法计算#define 术语?

发布于 2024-10-12 13:17:16 字数 86 浏览 3 评论 0原文

#define BLAH word

cout << BLAH;

有什么办法可以做到这一点吗?

#define BLAH word

cout << BLAH;

Is there any way to do this?

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

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

发布评论

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

评论(6

在巴黎塔顶看东京樱花 2024-10-19 13:17:16

Try

#define STRINGIFY(x) #x
#define STRINGIFYMACRO(y) STRINGIFY(y)

#define BLAH word
cout << STRINGIFYMACRO(BLAH);

额外的间接级别会导致宏的值而不是宏的名称被字符串化。

在 ideone 上测试

Try

#define STRINGIFY(x) #x
#define STRINGIFYMACRO(y) STRINGIFY(y)

#define BLAH word
cout << STRINGIFYMACRO(BLAH);

The extra level of indirection causes the value of the macro to be stringified instead of the name of the macro.

Tested on ideone.

剪不断理还乱 2024-10-19 13:17:16
  1. “cout”不是你做的事情。它是一个全局变量,是 std::ostream 类型的实例。您可以说“输出到cout”。

  2. #define 进行文本替换。它基本上与您在文本编辑器中使用搜索和替换将 BLAH 替换为 word 相同。因此,行 cout << BLAH; 变成 cout <<词;。如果它不起作用,那是因为 cout << word; 不是该范围内的有效语句。预处理器不关心任何周围的文本。它对代码的理解基本上为零(它知道如何标记代码,即如果您不输入任何空格,则将运算符和其他标点符号分开,但仅此而已。)

  1. 'cout' is not something you do. It is a global variable, which is an instance of the std::ostream type. You could say e.g. "output to cout".

  2. #define does textual substitution. It is basically the same as if you used search-and-replace in your text editor to replace BLAH with word. Thus, the line cout << BLAH; turns into cout << word;. If it's not working, it's because cout << word; isn't a valid statement in that scope. The preprocessor does not care about any of the surrounding text. It has basically zero understanding of the code (it knows how to tokenize the code, i.e. break apart operators and other punctuation if you don't put in any space, but that's about it.)

流年已逝 2024-10-19 13:17:16

如果你想打印单词“word”,你可以这样做:

#define STRINGIFY(x) #x

cout << STRINGIFY(word);

If you want to print the word "word", you can do:

#define STRINGIFY(x) #x

cout << STRINGIFY(word);
装纯掩盖桑 2024-10-19 13:17:16

我怀疑您想要这样的东西:

#include <typeinfo>

template <typename T>
void print_type()
{
    std::cout << typeid(T).name() << std::endl;
}

int main()
{
    print_type<float>();
    print_type<int>();
}

请注意,typeid(T).name() 的值是实现定义的,并且可能什么都没有。如果不自己为每种类型编写函数,就无法以有保证的方式打印出类型。

您也可以进行重载来推断表达式的类型:(

#include <typeinfo>

template <typename T>
void print_type(const T&)
{
    std::cout << typeid(T).name() << std::endl;
}

int main()
{
    print_type(5.0f);
    print_type(5.0);
}

请注意,这会计算表达式,这是不必要的,但我怀疑这是一个问题。)

I suspect you want something like this:

#include <typeinfo>

template <typename T>
void print_type()
{
    std::cout << typeid(T).name() << std::endl;
}

int main()
{
    print_type<float>();
    print_type<int>();
}

Note that value of typeid(T).name() is implementation defined, and may be nothing at all. There is no way to print out a type in a guaranteed way without writing a function yourself, for each type.

You can make an overload that deduces the type of the expression too:

#include <typeinfo>

template <typename T>
void print_type(const T&)
{
    std::cout << typeid(T).name() << std::endl;
}

int main()
{
    print_type(5.0f);
    print_type(5.0);
}

(Note that this evaluates the expression, which is unnecessary, but I doubt that is a concern.)

ぶ宁プ宁ぶ 2024-10-19 13:17:16

我正在测试一个程序,用于不同的数字表示,即浮点型和双精度型。当定义的术语是浮动时,我希望它打印出:“测试浮动”。

无需滥用宏作为原始 typedef:

template<class T>
void do_test(char const *name) {
  std::cout << "Testing " << name << "...\n";
  T var = foo();
  bar(var);
}

int main() {
  do_test<float>("single precision");
  do_test<double>("double precision");
  return 0;
}

请注意,这可以让您为每个测试指定不同的名称(希望更有意义),而不仅仅是将测试的参数字符串化,但“float”和“double”可以是名称,如果您喜欢。

如果您真的非常想对参数进行字符串化,或者您只是对宏感到好奇:

#define DO_TEST(T) do_test<T>(#T)
int main() {
  DO_TEST(float);
  DO_TEST(double);
  return 0;
}

I'm testing a program for different representations of number, i.e. float and double. I want it to print out: "Testing for float" when the defined term is float.

No need to abuse macros as a primitive typedef:

template<class T>
void do_test(char const *name) {
  std::cout << "Testing " << name << "...\n";
  T var = foo();
  bar(var);
}

int main() {
  do_test<float>("single precision");
  do_test<double>("double precision");
  return 0;
}

Notice this lets you give different names (hopefully more meaningful) to each test, rather than just stringizing the parameters to the test, but "float" and "double" could be the names if you like.

If you really, really wanted to stringize the parameters – or if you're just curious about macros:

#define DO_TEST(T) do_test<T>(#T)
int main() {
  DO_TEST(float);
  DO_TEST(double);
  return 0;
}
睫毛溺水了 2024-10-19 13:17:16

好的,让我们根据您的评论再试一次:

我正在测试一个程序,用于不同的数字表示,即浮点型和双精度型。当定义的术语是浮动时,我希望它打印出:“测试浮动”。

听起来这就是你真正的意思:

我有如下代码:

cout << "As a float:" << float(value) << endl;
cout << "As a double:" << double(value) << endl;

我想为每一行创建一个宏,以便文本与类型转换相匹配。

在这种情况下,你想要这样的东西:

#define DEBUG(t, x) cout << "As a " #t ":" << t(x) << endl

DEBUG(float, value);
DEBUG(double, value);

OK, let's try this again, based on your comment:

I'm testing a program for different representations of number, i.e. float and double. I want it to print out: "Testing for float" when the defined term is float.

It sounds like this is what you really mean:

I have code like the following:

cout << "As a float:" << float(value) << endl;
cout << "As a double:" << double(value) << endl;

and I want to make a macro for each of these lines so that the text matches the type-cast.

In that case, you want something like this:

#define DEBUG(t, x) cout << "As a " #t ":" << t(x) << endl

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