C/CPP __restrict__ (Implementation Specific) Keyword

from : What does the restrict keyword mean in C++? - Stack Overflow

I was always unsure, what does the restrict keyword mean in C++?

Does it mean the two or more pointer given to the function does not overlap? What else does it mean?

===> a pointer with "restrict" keyword or the implementation/compiler specific version of it, means that the content refered to by the pointer can and only can be accessed through the said pointer.

c++restrict-qualifier

Follow

  • 34

    restrict is a c99 keyword. Yes, Rpbert S. Barnes, I know that most compilers support __restrict__. You will note that anything with double underscores is, by definition, implementation specific and thus NOT C++, but a compiler specific version of it. 

    – KitsuneYMG

     Jan 6 '10 at 9:31
  • 8

    What? Just because it's implementation specific does not make it not C++; the C++ allows for implementation specific stuff explicitly, and does not disallow it or render it not C++. 

    – Alice

     May 29 '14 at 5:02
  • 7

    @Alice KitsuneYMG means that it's not part of ISO C++, and is instead considered a C++ extension. Compiler creators are allowed to make and distribute their own extensions, which coexist with ISO C++ and act as part of a usually-less-or-non-portable unofficial addition to C++. Examples would be MS's old Managed C++, and their more recent C++/CLI. Other examples would be preprocessor directives and macros supplied by some compilers, such as the common #warning directive, or the function signature macros (__PRETTY_FUNCTION__ on GCC, __FUNCSIG__ on MSVC, etc.). 

    – Justin Time - Reinstate Monica

     Mar 17 '16 at 21:57 
  • 6

    @Alice To my knowledge, C++11 doesn't mandate full support for all of C99, nor do C++14 or what I know of C++17. restrict isn't considered a C++ keyword (see en.cppreference.com/w/cpp/keyword ), and in fact, the only mention of restrict in the C++11 standard (see open-std.org/jtc1/sc22/wg21/docs/papers/2012/n3337.pdf , a copy of the FDIS with minor editorial changes, §17.2 [library.c], PDF page 413) states that: 

    – Justin Time - Reinstate Monica

     Mar 20 '16 at 17:10
  • 6

    @Alice How so? I stated the part that says that restrict is to be omitted from (excluded from, left out of) C standard library function signatures and semantics when those functions are included in the C++ standard library. Or in other words, I stated the fact that says that if a C standard library function's signature contains restrict in C, the restrict keyword must be removed from the C++ equivalent's signature. 

    – Justin Time - Reinstate Monica

(Most Voted Answer)

In his paper, Memory Optimization, Christer Ericson says that while restrict is not part of the C++ standard yet, that it is supported by many compilers and he recommends it's usage when available:

restrict keyword

! New to 1999 ANSI/ISO C standard

! Not in C++ standard yet, but supported by many C++ compilers

! A hint only, so may do nothing and still be conforming

A restrict-qualified pointer (or reference)...

! ...is basically a promise to the compiler that for the scope of the pointer, the target of the pointer will only be accessed through that pointer (and pointers copied from it).

In C++ compilers that support it it should probably behave the same as in C.

See this SO post for details: Realistic usage of the C99 ‘restrict’ keyword?

Take half an hour to skim through Ericson's paper, it's interesting and worth the time.

Edit

I also found that IBM's AIX C/C++ compiler supports the __restrict__ keyword.

g++ also seems to support this as the following program compiles cleanly on g++:

#include <stdio.h>

int foo(int * __restrict__ a, int * __restrict__ b) {
    return *a + *b;
}

int main(void) {
    int a = 1, b = 1, c;

    c = foo(&a, &b);

    printf("c == %d\n", c);

    return 0;
}

I also found a nice article on the use of restrict:

Demystifying The Restrict Keyword

Edit2

I ran across an article which specifically discusses the use of restrict in C++ programs:

Load-hit-stores and the __restrict keyword

Also, Microsoft Visual C++ also supports the __restrict keyword.

The Memory Optimisation paper link is dead, here's a link to the audio from his GDC presentation. gdcvault.com/play/1022689/Memory 

– Grimeh

 Dec 8 '16 at 20:21

  • 1

    @EnnMichael: Obviously if you're going to use it in a portable C++ project, you should #ifndef __GNUC__ #define __restrict__ /* no-op */ or similar. And define it to __restrict if _MSC_VER is defined. 

    – Peter Cordes

     Apr 27 '17 at 22:43
FAILED: CSW/Basis/Math/CMakeFiles/UT_Math.dir/Test/src/ut_common_basis_math_random.cpp.o /usr/bin/c++ -I/home/gitlab-runner/builds/fAoajnyk/0/csw/Common/Common2/CSW/Basis/Math/include -I/home/gitlab-runner/builds/fAoajnyk/0/csw/Common/Common2/CSW/Basis/Math/Test/include -I/home/gitlab-runner/builds/fAoajnyk/0/csw/Common/Common2/CSW/include/Basis/Math -I/home/gitlab-runner/builds/fAoajnyk/0/csw/Common/Common2/CSW/include/Macro -I/home/gitlab-runner/builds/fAoajnyk/0/csw/Common/Common2/Core/Logger/../include/Logger -I/home/gitlab-runner/builds/fAoajnyk/0/csw/Common/Common2/Core/Macro/../include/Macro -isystem /home/gitlab-runner/builds/fAoajnyk/0/csw/Common/Common2/Core/OTSS/GoogleTest/googletest/include -isystem /home/gitlab-runner/builds/fAoajnyk/0/csw/Common/Common2/Core/OTSS/GoogleTest/googletest -O3 -DNDEBUG -fPIE -Wall -Wextra -Wpointer-arith -MD -MT CSW/Basis/Math/CMakeFiles/UT_Math.dir/Test/src/ut_common_basis_math_random.cpp.o -MF CSW/Basis/Math/CMakeFiles/UT_Math.dir/Test/src/ut_common_basis_math_random.cpp.o.d -o CSW/Basis/Math/CMakeFiles/UT_Math.dir/Test/src/ut_common_basis_math_random.cpp.o -c /home/gitlab-runner/builds/fAoajnyk/0/csw/Common/Common2/CSW/Basis/Math/Test/src/ut_common_basis_math_random.cpp In file included from /home/gitlab-runner/builds/fAoajnyk/0/csw/Common/Common2/CSW/include/Basis/Math/common_basis_math_matrix.h:22:0, from /home/gitlab-runner/builds/fAoajnyk/0/csw/Common/Common2/CSW/include/Basis/Math/common_basis_math_quaternion.h:22, from /home/gitlab-runner/builds/fAoajnyk/0/csw/Common/Common2/CSW/include/Basis/Math/common_basis_math_posequaternion.h:20, from /home/gitlab-runner/builds/fAoajnyk/0/csw/Common/Common2/CSW/include/Basis/Math/common_basis_math_path.h:24, from /home/gitlab-runner/builds/fAoajnyk/0/csw/Common/Common2/CSW/include/Basis/Math/common_basis_math.h:20, from /home/gitlab-runner/builds/fAoajnyk/0/csw/Common/Common2/CSW/Basis/Math/Test/include/ut_common_basis_math_random.h:20, from /home/gitlab-runner/builds/fAoajnyk/0/csw/Common/Common2/CSW/Basis/Math/Test/src/ut_common_basis_math_random.cpp:17: /home/gitlab-runner/builds/fAoajnyk/0/csw/Common/Common2/CSW/include/Basis/Math/common_basis_math_storage.h: In instantiation of ‘void csw::ResizableVector<ElementType, INITIAL_SIZE>::reserve(int) [with ElementType = double; int INITIAL_SIZE = 64]’: /home/gitlab-runner/builds/fAoajnyk/0/csw/Common/Common2/CSW/include/Basis/Math/common_basis_math_storage.h:268:16: required from ‘void csw::ResizableVector<ElementType, INITIAL_SIZE>::resize(int) [with ElementType = double; int INITIAL_SIZE = 64]’ /home/gitlab-runner/builds/fAoajnyk/0/csw/Common/Common2/CSW/include/Basis/Math/common_basis_math_matrix.h:92:36: required from here /home/gitlab-runner/builds/fAoajnyk/0/csw/Common/Common2/CSW/include/Basis/Math/common_basis_math_storage.h:215:27: error: ‘memcpy’ was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive] memcpy(new_value, m_append_value, sizeof(ElementType) * (m_size - m_prealloc_size)); ~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ In file included from /usr/include/features.h:424:0, from /usr/include/x86_64-linux-gnu/c++/7/bits/os_defines.h:39, from /usr/include/x86_64-linux-gnu/c++/7/bits/c++config.h:533, from /usr/include/c++/7/utility:68, from /usr/include/c++/7/array:38, from /home/gitlab-runner/builds/fAoajnyk/0/csw/Common/Common2/CSW/include/Basis/Math/common_basis_math_q.h:20, from /home/gitlab-runner/builds/fAoajnyk/0/csw/Common/Common2/CSW/include/Basis/Math/common_basis_math_timed.h:23, from /home/gitlab-runner/builds/fAoajnyk/0/csw/Common/Common2/CSW/include/Basis/Math/common_basis_math_path.h:22, from /home/gitlab-runner/builds/fAoajnyk/0/csw/Common/Common2/CSW/include/Basis/Math/common_basis_math.h:20, from /home/gitlab-runner/builds/fAoajnyk/0/csw/Common/Common2/CSW/Basis/Math/Test/include/ut_common_basis_math_random.h:20, from /home/gitlab-runner/builds/fAoajnyk/0/csw/Common/Common2/CSW/Basis/Math/Test/src/ut_common_basis_math_random.cpp:17: /usr/include/x86_64-linux-gnu/bits/string_fortified.h:31:1: note: ‘void* memcpy(void*, const void*, size_t)’ declared here, later in the translation unit __NTH (memcpy (void *__restrict __dest, const void *__restrict __src,如何解决
最新发布
08-16
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值