Type emulating a C++ reference better than std::reference_wrapper

折月煮酒 提交于 2019-11-28 01:38:08
template <typename T>
class Tref {
    std::tuple<T &> t;
public:
    template <typename... Args>
    Tref(Args&&... args): t(std::forward<Args>(args)...) {}
    operator T &() { return std::get<0>(t); }
    decltype(&std::get<0>(t)) operator&() { return &std::get<0>(t); }
};


Tref<double &> t(a);
t = b;
assert(&t != &b);

What std::reference_wrapper does is binding when the template erase the reference. You needn't build a class to meet your needs, and just use normal reference. (I may not describe clearly. My English is poor.)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!