如果您打算暴露變量的每個引用,請暴露該變量。
現在p.value_
正是你想要的。
除非是最簡單的方法就是寫:
template< class A, class B >
using same_test = std::enable_if_t< std::is_same< std::decay_t<A>, std::decay_t<B> >{}, int>
那麼類裏面寫:
template<class Self, same_test<Self, Parameter> =0>
friend decltype(auto) get_value(Self&& self) {
return decltype(self)(self).value_;
}
現在get_value(p)
將返回p.value
用正確的L/R/CV資格。
如果你不喜歡的語法,我可以爲您提供(p->*value)()
:
template<class F>
struct magic_method_ptr_t {
F f;
template<class Lhs>
friend auto operator->*(Lhs&& lhs, magic_method const& m) {
return [&](auto&&...args)->decltype(auto) {
return m.f(decltype(lhs)(lhs), decltype(args)(args)...);
};
}
};
template<class F>
magic_method_ptr_t<F> magic_method_ptr(F f) { return {std::move(f)}; }
auto value = magic_method_ptr([](auto&& x){ return get_value(decltype(x)(x)); }
一下添加到get_value
以上的情況下,和(p->*value)()
作品。
我不能爲您提供p.value()
語法,禁止醜陋的宏。
你經常使用'常量volatile'對象?我從來沒有見過。 –
你可能想寫一個簡單的代碼生成器。 – Arun
我用宏做了類似的事情。 –