C11型通用前pressions - 为什么不只是添加函数重载? [英] C11 type-generic expressions - why not just add function overloading?

查看:140
本文介绍了C11型通用前pressions - 为什么不只是添加函数重载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是读上 C11 维基百科的文章,C标准的新版本发布2011年12月,和我看到的附加功能之一是类型的通用前pressions


  

使用关键字_Generic型通用前pressions。例如,该
  下面的宏CBRT(X)转换为cbrtl(X),CBRT(x)或cbrtf(X)
  这取决于x的类型:

 的#define CBRT(X)_Generic((X),长双:cbrtl,\\
                              默认:CBRT,\\
                              飘:cbrtf)(X)


这看起来pretty可怕的我 - 如果他们要改变反正语言,为什么不只是添加函数重载像C ++


解决方案

C有外部符号一个命名空间,并应用ODR使得两个的extern 与相同的对象名字有两种翻译单元必须有相同的定义。

尽管可以创建一个C ABI的支持重载,C的主要优势是其简单的ABI。在几乎所有平台中的ABI是C ABI,以及它在执行一定的作用无论源语言。如果符号必须包括类型信息这会丢失。

TGE(如所使用的库)只是名称重整的一个手动操作的版本。它(或会做,有时在很可能不久的将来),它需要做的工作,让的typedef 声明来控制代数学密集型内部循环。谁需要像C语言的功能,人们++必须移植到C ++。

I was just reading the Wikipedia article on C11, the new version of the C standard released in Dec 2011, and I saw that one of the added features was "type-generic expressions":

Type-generic expressions using the _Generic keyword. For example, the following macro cbrt(x) translates to cbrtl(x), cbrt(x) or cbrtf(x) depending on the type of x:

#define cbrt(X) _Generic((X), long double: cbrtl, \
                              default: cbrt, \
                              float: cbrtf)(X)

This looks pretty horrible to me - if they are going to change the language anyways, why not just add function overloading like in C++?

解决方案

C has one namespace for external symbols, and applies the ODR such that two extern objects with the same name in two translation units must have the same definition.

Although it's possible to create a C ABI that supports overloading, the main strength of C is its ABI simplicity. On almost all platforms "the" ABI is the C ABI, and it plays some role in execution no matter the source language. This would be lost if symbols had to include type information.

TGE (as used by the library) is just a manually-operated version of name mangling. It does (or will do, sometime in the possibly very distant future) the job it needs to do, to allow typedef declarations to control generation of math-intensive inner loops. People who need the features of a language like C++ should port to C++.

这篇关于C11型通用前pressions - 为什么不只是添加函数重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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