Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rttr::variant why only const getter? #108

Closed
gabyx opened this issue Jan 24, 2018 · 1 comment · Fixed by #129
Closed

rttr::variant why only const getter? #108

gabyx opened this issue Jan 24, 2018 · 1 comment · Fixed by #129

Comments

@gabyx
Copy link
Contributor

gabyx commented Jan 24, 2018

why can we not get the object as non-const reference?

rttr::variant instance = rttr::type::get_by_name("MyMessage").create();
const MyMessage& r = instance.get_value<MyMessage>();

I could use the policy as rttr::policy::ctor::as_std_shared_ptr or rttr::policy::ctor::as_raw_ptr.
then get the pointer (MyMessage* & const) . Is there a reason, why it returns a const T&?

Would it be possibe to have a policy which constructs a std::unique_ptr? Probably its not so easy as std::move is needed internally when we get the object.

std::unique_ptr r =  instance.get_value<MyMessage>();

-> SFINAE which enables a get_value function which std::moves the stuff out?

Background:
I try to use this rttr stuff for serializing/deserializing messages, (serialize the type, here the MyMessage string, and the payload)

@acki-m
Copy link
Contributor

acki-m commented Jan 25, 2018

Regarding unique_ptr take a look at #10
But I am open for other workarounds.
Regarding the non-const-getter. I think I can add one.
For the moment I don't see a problem. Don't know what the original reason was anymore.

Mhm... mabye I can add a support for moving values out. However, the variant class will always store only copy constructible values.

acki-m added a commit to acki-m/rttr that referenced this issue Mar 5, 2018
Added 4 overloads of the method "variant_cast"
In order to move values out, to return values by reference
or to return a pointer to the underlying values, when the type does match:

template<class T>
T variant_cast(variant&& operand);

template<class T>
T variant_cast(variant& operand);

template<class T>
T variant_cast(const variant& operand);

template<class T>
const T* variant_cast(const variant* operand) RTTR_NOEXCEPT;

template<class T>
T* variant_cast(variant* operand) RTTR_NOEXCEPT;

Additionally:
- added non-const version of variant::get_value()
- fixed copy-right year in root CMakeLists.txt file

Fixes rttrorg#108
acki-m added a commit that referenced this issue Mar 8, 2018
* Added global "variant_cast" method

Added 4 overloads of the method "variant_cast"
In order to move values out, to return values by reference
or to return a pointer to the underlying values, when the type does match:

template<class T>
T variant_cast(variant&& operand);

template<class T>
T variant_cast(variant& operand);

template<class T>
T variant_cast(const variant& operand);

template<class T>
const T* variant_cast(const variant* operand) RTTR_NOEXCEPT;

template<class T>
T* variant_cast(variant* operand) RTTR_NOEXCEPT;

Additionally:
- added non-const version of variant::get_value()
- fixed copy-right year in root CMakeLists.txt file

Fixes #108
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants