C restrict with typedef
Asked Answered
C

3

1

i'm doing some code now and got some problem using restrict keyword.

typedef int* pt;

int foo(pt a, pt b)
{
 ... /* stuff */
}

What if I want to make a and b restricted? The code below failed:

typedef int* pt;

int foo(pt restrict a, pt restrict b)
{
 ... /* stuff */
}

Thanks in advance.

Computerize answered 13/11, 2010 at 23:38 Comment(2)
you need to be more precise what you mean by "The code below failed".Arun
FWIW, your code compiles fine with GCC.Arun
C
0

You need a "restricted pointer to integer" int * restrict p not a "pointer to restricted integer" restrict int *p so you will need to make another typedef. You can't "reach inside" the original one.

EDIT: While it's true that you can't reach inside the typedef and the modifier will always apply at the top level, in this case it turns out that you want the restrict at the top level. It's the inverse of what people usually run into with const: typedef char *char_ptr means const char_ptr (or char_ptr const, they're equivalent) both mean "constant pointer to char" not "pointer to constant char" which is what people want. (See also this SO thread: C++ typedef interpretation of const pointers )

So in this case I think typedef int *pt does mean that restrict pt means int * restrict pt. It's pretty easy to verify because gcc will complain about "invalid use of 'restrict'" for restrict int *x but not for restrict pt x.

Crush answered 13/11, 2010 at 23:40 Comment(4)
o god... i'm gonna need alot of hacks thenComputerize
can you please clariy? Why wouldn't pt restrict denote the type int *restrict ? I think this answer is wrong.Arun
@Johannes: assuming restrict "spreads" the same as const, you are right: static_assert(std::is_same<const pt, int* const>::value, "foobar"); does not complain.Branscum
I tried to update this to be correct after it was accepted but problems were pointed out in the answer. To those who would still downvote: At least comment with your reasoning since it's still going to be at the top and might as well be updated to fix any problems!Crush
G
2

Make sure you're compiling it using the C99 flag for your compiler. The restrict keyword doesn't exist in C89 C.

Gigantean answered 13/11, 2010 at 23:42 Comment(1)
Do you mean ANSI C99 or ANSI C89?Arun
N
1

Having a quick look and reading this similar SO question, the code would be, as the keyword 'restrict' is not reserved keyword in C++ compilers, as indicated by the accepted answer in the above linky, either __restrict or __restricted__, again, check your compiler...

typedef int* __restrict pt;

int foo(pt a, pt b)
{
 ... /* stuff */
}
Nubble answered 13/11, 2010 at 23:43 Comment(1)
I used your answer in a question and everyone is saying it's a very bad idea. Can you comment on my question?Misgiving
C
0

You need a "restricted pointer to integer" int * restrict p not a "pointer to restricted integer" restrict int *p so you will need to make another typedef. You can't "reach inside" the original one.

EDIT: While it's true that you can't reach inside the typedef and the modifier will always apply at the top level, in this case it turns out that you want the restrict at the top level. It's the inverse of what people usually run into with const: typedef char *char_ptr means const char_ptr (or char_ptr const, they're equivalent) both mean "constant pointer to char" not "pointer to constant char" which is what people want. (See also this SO thread: C++ typedef interpretation of const pointers )

So in this case I think typedef int *pt does mean that restrict pt means int * restrict pt. It's pretty easy to verify because gcc will complain about "invalid use of 'restrict'" for restrict int *x but not for restrict pt x.

Crush answered 13/11, 2010 at 23:40 Comment(4)
o god... i'm gonna need alot of hacks thenComputerize
can you please clariy? Why wouldn't pt restrict denote the type int *restrict ? I think this answer is wrong.Arun
@Johannes: assuming restrict "spreads" the same as const, you are right: static_assert(std::is_same<const pt, int* const>::value, "foobar"); does not complain.Branscum
I tried to update this to be correct after it was accepted but problems were pointed out in the answer. To those who would still downvote: At least comment with your reasoning since it's still going to be at the top and might as well be updated to fix any problems!Crush

© 2022 - 2024 — McMap. All rights reserved.