11

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; };

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

Nicol Bolas
  • 449,505
  • 63
  • 781
  • 982
Anycorn
  • 50,217
  • 42
  • 167
  • 261
  • I guess, it compiles if I do `typedef const double * __restrict type;` but does it create restrict double* or some sort of restrict applied to `type`? – Anycorn May 01 '10 at 03:56
  • Try and see. `restrict` isn't defined by the C++ standard so ymmv. If I recall my own experience correctly, restrict does participate in typedefs in GCC. – Potatoswatter May 01 '10 at 04:06
  • @P how can I do that? look at assembly directly and see the difference? restrict is in c99, I thought __restrict was C++? – Anycorn May 01 '10 at 04:08
  • There is a good chance that any C++ compiler that implements `restrict` type functionality will use the C specification of `restrict`; you might consider consulting the C standard. – James McNellis May 01 '10 at 04:15
  • 1
    @aaa: The best way is to look at assembly or design an experiment to differentiate. For example, if `restrict` enables the vectorizer, you can see a performance difference. For what it's worth, C does specify that `restrict` modifies a type (and a typedef) just like `const` does. – Potatoswatter May 01 '10 at 04:21
  • C standard says that restrict its a type qualifier, so if I understand correctly, typedef with restrict is okay? I cannot however use restrict with g++, I must use __restrict instead – Anycorn May 01 '10 at 04:22
  • @aaa by the way, any keyword beginning with two underscores is non-standard. (Macros like `__LINE__` and `__FUNCTION__` the only exceptions.) Also, you need at least three letters of my name `@pot` to deliver a message to me. – Potatoswatter May 01 '10 at 04:23
  • 2
    @Potato thank you. should be easy to test I guess. __restrict works across Intel and g++ C++ code, while restrict requires c99 mode, which g++ does not like. on Intel, -restrict allows to use restrict in C++ mode – Anycorn May 01 '10 at 04:25

1 Answers1

21

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.

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 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();
}

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.

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.

Community
  • 1
  • 1
mbauman
  • 30,958
  • 4
  • 88
  • 123
  • "C99 defines typedef similarly, except that its list of qualifiers includes restrict." I made [a question about this](http://stackoverflow.com/q/43631062/2542702) and everyone (that has commented so far) thinks it's a bad idea. Can you comment? Is it legal C to typedef a pointer with restrict? – Z boson Apr 27 '17 at 07:07