【发布时间】:2013-06-25 18:39:00
【问题描述】:
据我了解restrict,它将指针标记为对函数中特定数据的唯一引用。我经常看到它用在函数参数中,但这似乎也是有益的:
char *restrict a = get_some_string( );
char *restrict b = get_some_other_string( );
(因此编译器知道更改a 永远不会更改b,并且可以进行额外的优化)。
如果get_some_string返回一个很复杂的类型,似乎最好使用auto关键字;
auto a = get_some_string( );
auto b = get_some_other_string( );
但是使用auto restrict 会触发错误“restrict requires a pointer”。那么,我该如何结合这些呢?
如 cmets 中所述,restrict 不是 C++ 中的标准关键字;我忘记了我的项目中有一个 #define restrict __restrict__ 行,它适用于 GCC。
【问题讨论】:
-
decltype( get_some_string( ) ) restrict a = get_some_string( );?虽然 decltype 的行为略有不同。 -
替代建议:使用
typedef,而不是typedef。 -
@JohnDibling
typedefs 是我以前使用的。但我正在努力跟上时代的步伐并简化我的代码! -
@indeterminatelysequenced 似乎有同样的问题; “限制需要一个指针”
-
@Dave:我个人不是
auto的忠实粉丝,因为在我看来,滥用和语义模糊的可能性很大。其他人会不同意。
标签: c++ c++11 auto restrict-qualifier