Boost::any typecasting issue(bad_any_cast)
Hi Team, While doing typecasting from boost::any to DecodeErrorPayload_t, its throwing bad_any_cast .. class RegisterIONotification { public: enum NotifyType { TypeUnknown, TypeRegTx, TypeRegRx, TypeDecodeError }; NotifyType Type; boost::any Payload; typedef boost::tuple<std::string, BinaryBuffer_t> DecodeErrorPayload_t; RegisterIONotification(NotifyType type;,boost::any payload) { Type(type); Payload(payload); } }; I have called RegisterIONotification from other class function. void RegisterIOThreaded::OnDecodeError( const std::string& error, BinaryBuffer_t data ) { RegisterIONotification notify(RegisterIONotification::TypeDecodeError ,make_tuple(error, data)); ..... .... ... } While testing, I am getting bad_any_cast exception. TEST_F(...,...) { RegisterIONotification notify = sink.GetDataById(ID::NOTIFY_COMMS_STATUS, 0).GetValue<RegisterIONotification>(); ASSERT_EQ(RegisterIONotification::TypeDecodeError, notify.Type); //passed RegisterIONotification::DecodeErrorPayload_t payload; ASSERT_NO_THROW(payload = boost::any_cast<RegisterIONotification::DecodeErrorPayload_t>(notify.Payload));// Here its got failed...Please help me to fix this. } Regards, Rajesh D Please be advised that this email may contain confidential information. If you are not the intended recipient, please notify us by email by replying to the sender and delete this message. The sender disclaims that the content of this email constitutes an offer to enter into, or the acceptance of, any agreement; provided that the foregoing does not invalidate the binding effect of any digital or other electronic reproduction of a manual signature that is included in any attachment.
On Sat, Dec 4, 2021, 00:48 Daggumati, Rajesh via Boost < boost@lists.boost.org> wrote:
Hi Team,
While doing typecasting from boost::any to DecodeErrorPayload_t, its throwing bad_any_cast ..
Please, provide a minimal example that *compiles, links* and reproduces the problem. Regards,
Rajesh D Please be advised that this email may contain confidential information. If you are not the intended recipient, please notify us by email by replying to the sender and delete this message. The sender disclaims that the content of this email constitutes an offer to enter into, or the acceptance of, any agreement; provided that the foregoing does not invalidate the binding effect of any digital or other electronic reproduction of a manual signature that is included in any attachment.
You are sending emails to a public mailing list that is available for anonymous reading from all over the world.
On 19/11/2021 04:57, Daggumati, Rajesh wrote:
typedef boost::tuple<std::string, BinaryBuffer_t> DecodeErrorPayload_t; RegisterIONotification(NotifyType type;,boost::any payload)
[...]
void RegisterIOThreaded::OnDecodeError( const std::string& error, BinaryBuffer_t data ) { RegisterIONotification notify(RegisterIONotification::TypeDecodeError ,make_tuple(error, data)); [...] ASSERT_NO_THROW(payload = boost::any_cast<RegisterIONotification::DecodeErrorPayload_t>(notify.Payload));// Here its got failed...Please help me to fix this.
You have to be *very* careful about using type inference and implicit casts when using boost::any. For example, it's entirely possible that make_tuple is returning a type that is compatible with DecodeErrorPayload_t but is not actually DecodeErrorPayload_t, which will cause your later any_cast to fail. (Especially if you're calling std::make_tuple rather than boost::make_tuple, which seems likely.) You can inspect the payload in a debugger to determine what type it actually is (although it can be hard to interpret). Or, (while undocumented) it does contain a type() method that you can call and log to inspect the actual type. You may get better results if you explicitly static_cast your make_tuple call to the type that you're expecting, or use uniform construction syntax instead of make_tuple.
participants (3)
-
Antony Polukhin
-
Daggumati, Rajesh
-
Gavin Lambert