As I understand restrict
, it marks a pointer as being the only reference to particular data within a function. I usually see it used in function parameters, but this is also seems to be beneficial:
char *restrict a = get_some_string( );
char *restrict b = get_some_other_string( );
(so the compiler knows that changing a
will never change b
, and can do extra optimisation).
If get_some_string
returns a very complicated type, it seems best to use the auto
keyword;
auto a = get_some_string( );
auto b = get_some_other_string( );
But using auto restrict
triggers the error "restrict requires a pointer". So, how can I combine these?
As noted in the comments, restrict
isn't a standard keyword in C++; I'd forgotten that I've got a #define restrict __restrict__
line in my project, which works in GCC.
Since a sort-of-solution has been suggested in the comments, I'll post it here for future reference; (with additions to make it robust)
typename std::remove_reference<decltype(get_some_string()[0])>::type *restrict a = get_some_string( );
It's horrific. I'll be sticking to typedef
s in these cases, but I can imagine there might be situations where behaviour like this is necessary. With a macro it becomes a bit less terrible:
#define decltype_restrict(x) typename std::remove_reference<decltype((x)[0])>::type *restrict
decltype_restrict(get_some_string()) a = get_some_string( );