C++宏:如何创建覆盖?

发布于 2024-10-20 23:22:27 字数 892 浏览 5 评论 0原文

所以我发现需要使用类似 Boost.Extension 让我的应用程序对新模块更加开放。但是当我到达第一个教程时,我发现发现它的语法完全不像我习惯的那样:

// Depending on the compiler and settings,
// it may be necessary to add a specific export
// declaration. The BOOST_EXTENSION_EXPORT_DECL
// adds this if necessary.
void BOOST_EXTENSION_EXPORT_DECL
boost_extension_hello_world(int repetitions) {
  for (int i = 0; i < repetitions; ++i) {
    std::cout << "Hello World" << std::endl;
  }
}

我希望可以编写类似 void function 的东西,而不是 void BOOST_EXTENSION_EXPORT_DECL 它看起来更好,并且因为我有 AS3 背景,对我来说看起来不会那么可怕。

那么如何在您自己的 C++ 文件中而不是在定义它的标头中创建 C++ 宏的覆盖呢?

So I found a need in using something like Boost.Extension to keep my apps more open to new modules. But as soon as I got to first tutorial I found out that its syntax is quite not like I'm used to:

// Depending on the compiler and settings,
// it may be necessary to add a specific export
// declaration. The BOOST_EXTENSION_EXPORT_DECL
// adds this if necessary.
void BOOST_EXTENSION_EXPORT_DECL
boost_extension_hello_world(int repetitions) {
  for (int i = 0; i < repetitions; ++i) {
    std::cout << "Hello World" << std::endl;
  }
}

I want to make it possible to write something like void function instead of void BOOST_EXTENSION_EXPORT_DECL it looks better and as i have AS3 background it will not look like something horrible for me.

So how to create an overrite for C++ macro not in header where it was defined but in your own C++ file?

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

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

发布评论

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

评论(3

温柔戏命师 2024-10-27 23:22:27

您可以在之前编写以下内容:

#define function BOOST_EXTENSION_EXPORT_DECL

然后像这样声明该函数:

void function
boost_extension_hello_world(int repetitions) {
  for (int i = 0; i < repetitions; ++i) {
    std::cout << "Hello World" << std::endl;
  }
}

You could just write the following before:

#define function BOOST_EXTENSION_EXPORT_DECL

And then state the function like this:

void function
boost_extension_hello_world(int repetitions) {
  for (int i = 0; i < repetitions; ++i) {
    std::cout << "Hello World" << std::endl;
  }
}
街角卖回忆 2024-10-27 23:22:27

您可以将 BOOST_EXTENSION_EXPORT_DECL 替换为 function

#define function BOOST_EXTENSION_EXPORT_DECL 

然后您可以通过以下方式使用它:

void function boost_extension_hello_world(int repetitions) {
  for (int i = 0; i < repetitions; ++i) {
    std::cout << "Hello World" << std::endl;
  }
}

但是,如果您的代码包含单词 function,这可能会导致问题,因为编译器替换所有出现的function

You can replace BOOST_EXTENSION_EXPORT_DECL by function with:

#define function BOOST_EXTENSION_EXPORT_DECL 

Then you can use it by:

void function boost_extension_hello_world(int repetitions) {
  for (int i = 0; i < repetitions; ++i) {
    std::cout << "Hello World" << std::endl;
  }
}

But this could cause problems if your code contains the word function, because the compiler replace all occurrence of function.

深海夜未眠 2024-10-27 23:22:27
#define function BOOST_EXTENSION_EXPORT_DECL
#define function BOOST_EXTENSION_EXPORT_DECL
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文