是的,
见this link
__declspec(property(get=..,put=..)) 得到 clang 的完全支持,这是 gcc 对 Microsoft 语言功能的支持的继承。
我一直在clang中使用它;它非常适合封装和重构。我帮助调试和推广了正确的 clang 实现。
使用array accessor properties 进行优化非常出色。
foo[expr0][expr1] = expr2;
foo 在哪里
__declspec(property(put=foo_set)) foo_t foo[];
foo_t foo_set(T0 expr0, T1 expr1, foo_t expr2) {..}
它还可以与模板函数完美配合,使其成为高效重载和前向引用的理想选择。
template<typename T0, typename T1, typename foo_ta = foo_t>
foo_ta foo_set(T0 expr0, T1 expr1, foo_ta expr2) {..}
唯一的糟糕是你不能使用现代c++ custom-attribute简写:
[[msvc::property(put = foo_set)]] foo_t foo[];
所以我使用这个模式:
[[msvc::property(put = foo_set)]] __declspec(property(put = foo_set))
foo_t foo[];
template<typename T0, typename T1, typename foo_ta = foo_t>
foo_ta foo_set(T0 expr0, T1 expr1, foo_ta expr2) {..}
或
template<bool fFwd=true>
bar_t bar_get() {
// reference any types declared later in your code
// template mechanics mean they will not be resolved until
// first **property** use
}
您不需要使用我上面展示的任何 template 用法或 array accessor 用法。我只是为了说明超越可以用属性做什么以及利用重载函数。
我使用-Wattributes 控制有关[[msvc::...]] 未定义的警告。使用这种模式,我的代码既可以为未来做好准备,又可以更清晰、更一致地阅读。
给定属性仅适用于实例。将它们放在类型上的技术是在类型上使用空单例:
struct T {
static inline struct K {
..declare properties on `k` here..
} k;
.. whatever you are doing with this `T` type.
};
现在您可以通过以下方式访问该类/静态属性:
T::k.property ..