Boost logo

Boost :

From: Andrey Semashev (andrey.semashev_at_[hidden])
Date: 2021-11-18 21:46:35


On 11/18/21 20:33, rajesh daggumati via Boost wrote:
> 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);
>
> }

This constructor doesn't make sense and it will fail to compile.

> };
>
>
>
> 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));

Which make_tuple is this?

> .....
>
> ....
>
> ...
>
> }
>
>
>
> While testing, I am getting bad_any_cast exception.
>
>
>
> TEST_F(...,...)
>
> {
>
> RegisterIONotification notify =
> sink.GetDataById(ID::NOTIFY_COMMS_STATUS,
> 0).GetValue<RegisterIONotification>();

How does the line above relate to the previous code you posted? How are
we supposed to know how `notify` was originally constructed?

> 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.
>
> }

Rajesh, please try to post code that actually makes sense and is
readable. Otherwise you are unlikely to get help.

I suggest you debug the problem on your end first. For example,
inspecting the Payload value (whether it is empty or not, through its
empty() method) and type (through its type() method) might give you a clue.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk