简体   繁体   English

在 C++ 中使用宏的可变参数

[英]Variadic argument using macros in C++

I have 2 platforms which require different library functions but I want to minimize the code required to support them.我有 2 个需要不同库函数的平台,但我想尽量减少支持它们所需的代码。

C++14 Code : C++14 代码:

#ifdef PLATFORM2
#define FUNCTION_AUTO unsigned int dmd
#else
#define FUNCTION_AUTO void
#endif

//-- WITHIN THE LIBRARY -- //
double getQuality(FUNCTION_AUTO){
  //Computing
  // return value;
}

//-- MAIN EXECUTABLE -- //
int main() {
  std::cout << "Hello World!\n";
#ifdef PLATFORM2
  getQuality(100);
#else
  getQuality();
  #endif
}

Is there a better way to define getQuality(..) to be common between the 2 platforms without using preprocessor manipulation ?有没有更好的方法来定义getQuality(..)在不使用预处理器操作的情况下在 2 个平台之间通用?

The normal approach here would be to abstract the platform specific bits into a library and have a consistent abstraction on top of them.这里的正常方法是将特定于平台的位抽象到一个库中,并在它们之上有一个一致的抽象。

platform.h平台.h

double Quality();

platform_a.cc平台_a.cc

#inclue "platform.h"

double Quality() {
  return getQuality();
}

platform_b.cc platform_b.cc

#include "platform.h"

double Quality() {
  return getQuality(100);
}

main.c主文件

#include <iostream>
#include "platform.h"

int main() {
  std::cout << Quality();
}

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

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