C / C ++ __restrict类型 [英] C/C++ __restrict type

查看:203
本文介绍了C / C ++ __restrict类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法使用,这意味着没有aliasng的typedef积分/浮点类型来定义?

Is there a way to define using typedef integral/float type which implies no aliasng?

等同的东西(但基本结构):

something equivalent to (but primitive construct):

template < typename T >
struct restrict { T* __restrict data; };

为相关的问题,是有可能要问的gcc是什么决定了别名/没有指针的别名是什么?

as related question, is it possible to ask gcc what it determines alias/no alias of pointer is?

推荐答案

由于在评论中指出,许多新的C ++编译器不支持C99实施限制类型限定符。由于限制是不是在C ++保留关键字,编译器一般使用 __限制 __限制__ 。这两个 GCC 并的Visual C ++ 这个文件很好,具有明确的C99引用。

As noted in the comments, many newer C++ compilers do support the C99 implementation of the restrict type qualifier. Since restrict is not a reserved keyword in C++, the compilers generally use __restrict or __restrict__. Both GCC and Visual C++ document this nicely, with explicit references to C99.

在C ++ 1998年标准规定的的typedef 说明不得......可以合并为一的 DECL说明符-SEQ 的任何种类除了一个的类型说明符的的。从本质上讲,它必须是列表的类型说明符的,其中包括两个 CV-预选赛常量挥发性

The C++ 1998 standard states that "The typedef specifier shall not ... be combined in a decl-specifier-seq with any kind of specifier except a type-specifier." Essentially, it must be a list of type-specifiers, which includes the two cv-qualifiers, const and volatile.

C99定义的typedef类似,除了其预选赛的名单中包括限制

C99 defines typedef similarly, except that its list of qualifiers includes restrict.

这似乎是合理的预见在类型定义类似的支持非标准 __限制 ...但你永远不知道!

It would seem reasonable to anticipate similar support in typedefs for the nonstandard __restrict... but you never know!

有一个巧妙而简单的方法来测试,这是如下:

A clever and easy way to test this is as follows:

extern void link_fail();

typedef int *__restrict restricted_int_p;

void test(restricted_int_p a, restricted_int_p b) {
    *a = 1;
    *b = 2;

    if (*a == 2) link_fail();
}

这只是利用了,如果未解决 link_fail 符号的目标文件中找到的事实,链接器将抛出一个错误。如果编译器正确限制两个参数的话,就应该知道 A ,即使在值被改变。因此,应剥去整个如果从生成的目标文件,因为它不会被运行块

This simply exploits the fact that if the unresolved link_fail symbol is found in the object file, the linker will throw an error. If the compiler is properly restricting the two arguments, then it should know the value of a, even after b is changed. Thus, it should strip the entire if block from the generated object file since it will never be run.

请注意,虽然GCC支持的限制,因为至少3.0版本的语法,它真的没有进行适当的优化,直到的 4.5版

Note that although GCC supported the restrict syntax since at least version 3.0, it really didn't perform the proper optimizations until version 4.5.

这篇关于C / C ++ __restrict类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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