C ++ 11 - 编译时间多态解决方案 [英] C++11 - Compile time Polymorphism solutions

查看:195
本文介绍了C ++ 11 - 编译时间多态解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我编写一个跨平台库,我必须以一种方式组织代码,使得不同平台有不同的行为,并且这个行为(或定义)在编译时根据平台进行选择

Suppose that I'm writing a cross-platform library, I have to organize the code in a way that there is a different behaviour for different platforms and this behaviour ( or definition ) it's choosen at compile time based on the platform where my library it's being compiled.

在C ++中执行此操作的常用方法是使用大量的 #ifdef

The "usual" way to do this in C++ is to pollute the code with a lot of #ifdef when writing a method or a class.

方法的问题是:


  • 源代码看起来真的很丑陋

  • 如果你支持3个平台,你的源代码比你真正需要的大3倍,意味着你的编译器仍然需要解析和分析所有代码以看到 #ifdef

  • ,在不同的实现之间没有真正的区别,

因为这里有一个代码库,所以当代码库不断增长时,是C ++ 11中的很多新特性我想知道是否有变化,如果有新的选择。

Since there are a lot of new features in C++11 I was wondering if something is changed and if there are new options for this.

推荐答案

p>你应该使用你的构建系统来做到这一点。您应该为头提供与平台无关的函数声明和类的定义。然后,根据目标平台,构建系统应该编译这些函数和类的适当实现。

You should be using your build system to do this. You should provide headers with platform-independent declarations of functions and definitions of classes. Then, depending on the target platform, the build system should compile the appropriate implementations of those functions and classes.

例如,让我们考虑创建窗口以显示图形或GUI元素。如果你没有使用库来做到这一点,你必须自己写跨平台的代码。首先,你应该仔细考虑平台无关接口应该是什么。也许你有一个窗口类和一些帮助函数。然后,您可以在头文件中提供该类的定义和辅助函数的声明,并为每个平台提供单独的实现。然后你会有一组文件像这样:

For example, let's consider the creation of windows for displaying graphics or GUI elements. If you're not using a library to do this, you have to write the cross-platform code yourself. First, you should think about exactly what the platform-independent interface should be. Perhaps you have a window class and some helper functions. You could then provide the definition for that class and the declarations of the helper functions in a header file and provide separate implementations for each platform. Then you'll have a set of files like this:


  • window.h

  • window_wayland.cpp

  • window_winapi.cpp

  • window_x11.cpp

  • window.h
  • window_wayland.cpp
  • window_winapi.cpp
  • window_x11.cpp

现在,所有需要使用你的类和函数的文件应该只是 #include< window.h> 。它们都获得相同的函数声明。但是,在构建系统的配置中指定 window_x11.cpp 应该在具有X11窗口系统的系统上编译, window_wayland.cpp 在具有Wayland的系统上, window_winapi 在Windows上。这意味着,根据您正在构建的平台,您将获得在目标平台上工作的标题的实现。

Now, all of the files that need to use your class and functions should just #include <window.h>. They all get the same function declarations. However, you specify in the configuration of your build system that window_x11.cpp should be compiled on systems with the X11 windowing system, window_wayland.cpp on systems with Wayland, and window_winapi on Windows. This means that depending on the platform you're building on, you will get an implementation of that header that works on the target platform.

这有很多优点:


  1. 您已从代码问题中分离出构建问题(您正在构建的平台)。

  2. <每个平台相关的实现都有自己的文件。
  3. 你没有一个文件混乱的预处理器指令,很难遵循执行路径。

这并不意味着使用define有选择地编译代码的不同部分有什么问题。我喜欢看到这只有少量的代码已经本地化到平台相关的部分。理想情况下,将平台相关代码包装在一个函数中,并让 #ifdef 只是交换执行。

This doesn't mean there's anything wrong with using defines to selectively compile different parts of your code. I prefer to see this only with small amounts of code that has been localized to the platform-dependent parts. Ideally, wrap the platform-dependent code in a function and have the #ifdefs just swap out the implementation.

正是你如何做这种选择性构建取决于你使用的构建系统。对于GNU构建系统,可以使用automake实现条件编译。 文档中提供了一些示例。一个简单的例子是:

Exactly how you do this selective building depends on the build system you're using. For the GNU build system, you can achieve conditional compilation with automake. Some examples are given in the documentation. A simple example given is:

bin_PROGRAMS = hello
if LINUX
hello_SOURCES = hello-linux.c hello-common.c
else
hello_SOURCES = hello-generic.c hello-common.c
endif

使用此配置运行 automake 会生成相应的makefile。

Running automake with this configuration will generate the appropriate makefile.

这篇关于C ++ 11 - 编译时间多态解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆