I'm creating a JSON library for C++14 and I'm trying to make use of move semantics whenever possible.
My Value
class has several setters and getters that always try to move when possible:
template<class T> void setObj(T&& x) { type = Obj; hObj.init(forward<T>(x)); }
template<class T> void setArr(T&& x) { type = Arr; hArr.init(forward<T>(x)); }
template<class T> void setStr(T&& x) { type = Str; hStr.init(forward<T>(x)); }
auto& getObj() & noexcept { assert(is<Obj>()); return hObj; }
auto& getArr() & noexcept { assert(is<Arr>()); return hArr; }
auto& getStr() & noexcept { assert(is<Str>()); return hStr; }
const auto& getObj() const& noexcept { assert(is<Obj>()); return hObj; }
const auto& getArr() const& noexcept { assert(is<Arr>()); return hArr; }
const auto& getStr() const& noexcept { assert(is<Str>()); return hStr; }
auto getObj() && noexcept { assert(is<Obj>()); return move(hObj); }
auto getArr() && noexcept { assert(is<Arr>()); return move(hArr); }
auto getStr() && noexcept { assert(is<Str>()); return move(hStr); }
As you can see from the code, perfect forwarding setter functions is pretty easy using templates and universal references.
How can I do the same for getter functions? I'm pretty sure I have to use a template return type but I'm not sure how to replicate ref-qualifiers and const-correctness.
Since you can't template on ref qualifiers and member constness, the sad answer is that you can't. You have to write them out.
This is not like C++ templates but it does the job.
#define GETTERS(V) \
V(Obj) \
V(Arr) \
V(Str)
#define VISIT(X) \
auto &get ## X() & noexcept { assert(is<X>()); return h ## Obj; } \
const auto &get ## X() const& noexcept { assert(is<X>()); return h ## Obj; } \
auto &get ## X() && noexcept { assert(is<X>()); return std::move(h ## Obj); }
GETTERS(VISIT)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With