简体   繁体   English

使用可变参数宏后如何保持原来的 function 返回值?

[英]How to keep original function return value after using Variadic Macros?

It is a VS2010 C++ project.这是一个VS2010 C++项目。 I have a list of APIs:我有一个 API 列表:

  1. int a = API1("para1", "para2", ...);

  2. double a = API2("para1", "para2", ...);

  3. float a = API3("para1", "para2", ...);

Now, I need to add a new API, APINEW() .现在,我需要添加一个新的 API APINEW() As long as above APIs are run, APINEW need to called as follow.只要运行上述 API,APINEW 就需要按如下方式调用。 Therefore, I decided to use the Variadic Macros as shown below:因此,我决定使用 Variadic Macros,如下所示:

#define newAPI1(...) ( API1(__VA_ARGS__); APINEW() )

#define newAPI2(...) ( API2(__VA_ARGS__); APINEW() )

#define newAPI3(...) ( API3(__VA_ARGS__); APINEW() )

However, I will not correctly get the return value from my API1, API2 and API3.但是,我不会正确地从我的 API1、API2 和 API3 获取返回值。

I am trying to use the following code since I know the marco will always return the last item, but it cannot pass the compile.我正在尝试使用以下代码,因为我知道 marco 将始终返回最后一项,但它无法通过编译。

#define newAPI1(...) ( {auto a = API1(__VA_ARGS__); APINEW(); a} )

I wonder is there a way that can make it correct?我想知道有没有一种方法可以使它正确? (The reason I use new name (eg newAPI1) is to avoid comflict because in the project, it may have other macros that overload the existing API1, API2 and API3.) (我使用新名称(例如newAPI1)的原因是为了避免冲突,因为在项目中,它可能有其他宏会重载现有的API1、API2和API3。)

Another question: is there a way to pass the combination of另一个问题:有没有办法通过组合

  1. first parameter of __VA_ARGS__ __VA_ARGS__的第一个参数
  2. __FUNCTION__
  3. __LINE__

into the APINEW parameter => APINEW(first parameter of __VA_ARGS__ + __FUNCTION__ + __LINE__)进入 APINEW 参数 => APINEW(first parameter of __VA_ARGS__ + __FUNCTION__ + __LINE__)

Something along these lines, perhaps.也许是这样的。

template <typename FirstArg, typename... Args>
auto API1Helper(const char* file, int line, FirstArg&& first_arg, Args&&... args) {
  auto ret = API1(first_arg, std::forward<Args>(args)...);
  APINEW(first_arg, file, line);
  return ret;
}

#define newAPI1(...) API1Helper(__FILE__, __LINE__, __VA_ARGS__)

Though this may require a compiler newer than VS2010.虽然这可能需要比 VS2010 更新的编译器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM