[boost] boost::any_cast gives build errors with VS2022, boost v1.82 and C++17

7 views
Skip to first unread message

Bhuyan, Jyotideep via Boost

unread,
Sep 29, 2023, 5:40:53 PM9/29/23
to bo...@lists.boost.org, Bhuyan, Jyotideep
Hi,

I was using VS2019, boost v1.78, and C++14 version and the below code was working well. Same code gives build error with VS2022, boost v1.82, and C++17 due to "o << *boost::any_cast<T>(&a);
" Instruction. I wanted to know why it behaves differently.

Code :

class any_out
{
struct streamer
{
virtual void print(std::ostream& o, const boost::any& a) = 0;
virtual streamer* clone() = 0;
virtual ~streamer() {}
};

public:
any_out() : m_streamer(nullptr)
{
}

~any_out()
{
delete m_streamer;
}

template <typename T> any_out(const T& value)
: m_streamer(new streamer_imp<T>), m_object(value)
{
}

any_out(const any_out& a)
: m_streamer(a.m_streamer ? a.m_streamer->clone() : nullptr), m_object(a.m_object)
{
}

template <typename T> any_out& operator=(const T& r)
{
any_out(r).swap(*this);
return *this;
}

any_out& operator=(const any_out& r)
{
any_out(r).swap(*this);
return *this;
}

any_out& swap(any_out& r) noexcept
{
std::swap(m_streamer, r.m_streamer);
std::swap(m_object, r.m_object);
return *this;
}

friend std::ostream& operator<<(std::ostream& o, const any_out& a)
{
if(a.m_streamer)
{
a.m_streamer->print(o, a.m_object);
}
return o;
}

boost::any& get() { return m_object; }
const boost::any get()const { return m_object; }
bool empty() const { return m_object.empty(); }

private:

template <typename T> struct streamer_imp : public streamer
{
virtual void print(std::ostream& o, const boost::any& a)
{
try {
if (a.empty()) {
o << "<empty>";
}
else {
o << *boost::any_cast<T>(&a);
}

}
}
virtual streamer* clone()
{
return new streamer_imp<T>();
}
};

// Members
streamer* m_streamer;
boost::any m_object;

};


I have migrated to VS2022, boost v1.82, and C++17, and due to a build error, I have changed the code below.

virtual void print(std::ostream& o, const boost::any& a)
{
try {
if (a.empty()) {
o << "<empty>";
}
else {
o << boost::any_cast<T*>(a);
}

}
}



any_out anyTest;
anyTest = 100;
std::string actualStr;
actualStr = make_string() << anyTest;

actualStr prints nullptr (0000000) instated of value 100 for VS2022, boost v1.82 and C++17





Best Regards,
Jyotideep (JD)


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.

Confidential - Company Proprietary

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

Andrzej Krzemienski via Boost

unread,
Sep 30, 2023, 2:26:26 AM9/30/23
to Boost mailing list, Andrzej Krzemienski
It looks like you are using boost::any incorrectly. You cannot know ahead
of time that an any is golding a T. If you want to use a T, you have to
check it. An any my be non-empty, but may be storing some other type U.
any_cast allows you to chech it. If it retuns a null pointer, it means you
are storing some other type than T.

Maybe you need boost:optional<T> rather than boost::any?

Regards,
&rzej;

Peter Dimov via Boost

unread,
Sep 30, 2023, 3:25:51 AM9/30/23
to bo...@lists.boost.org, Peter Dimov
Bhuyan, Jyotideep wrote:
> Hi,
>
> I was using VS2019, boost v1.78, and C++14 version and the below code was
> working well. Same code gives build error with VS2022, boost v1.82, and
> C++17 due to "o << *boost::any_cast<T>(&a);
> " Instruction. I wanted to know why it behaves differently.

Your code compiles and works for me with VS2022 and C++17 (after adding
a `catch` clause after the `try`, e.g. https://godbolt.org/z/eWv9sjYzz.)

What errors do you get?

Bhuyan, Jyotideep via Boost

unread,
Oct 1, 2023, 3:18:28 AM10/1/23
to bo...@lists.boost.org, Bhuyan, Jyotideep, Peter Dimov
This is strange ☹
By mistake, I removed the catch block from the example code. This was present in the original piece of code.

I am getting the compilation error with the new boost 1.82 and the VS2022 (v143 ) + C++17 is as follows at this line.
o << *boost::any_cast<T>(&a); //which was running with boost version 1.78 + VS2019 (v142) + C++14



Severity
Code
Description
Error
C2679
binary '<<': no operator found which takes a right-hand operand of type 'const ValueType' (or there is no acceptable conversion)



Best Regards,
Jyotideep (JD)



Confidential - Company Proprietary

From: Boost <boost-...@lists.boost.org> On Behalf Of Peter Dimov via Boost
Sent: Saturday, September 30, 2023 12:55 PM
To: bo...@lists.boost.org
Cc: Peter Dimov <pdi...@gmail.com>
Subject: Re: [boost] boost::any_cast gives build errors with VS2022, boost v1.82 and C++17

Bhuyan, Jyotideep wrote: > Hi, > > I was using VS2019, boost v1. 78, and C++14 version and the below code was > working well. Same code gives build error with VS2022, boost v1. 82, and > C++17 due to "o << *boost: : any_cast<T>(&a);
ZjQcmQRYFpfptBannerStart
This Message Is From an External Sender
This message came from outside your organization. Use caution when opening.
ZjQcmQRYFpfptBannerEnd

Bhuyan, Jyotideep wrote:

> Hi,

>

> I was using VS2019, boost v1.78, and C++14 version and the below code was

> working well. Same code gives build error with VS2022, boost v1.82, and

> C++17 due to "o << *boost::any_cast<T>(&a);

> " Instruction. I wanted to know why it behaves differently.



Your code compiles and works for me with VS2022 and C++17 (after adding

a `catch` clause after the `try`, e.g. https://urldefense.proofpoint.com/v2/url?u=https-3A__godbolt.org_z_eWv9sjYzz&d=DwICAg&c=9mghv0deYPYDGP-W745IEdQLV1kHpn4XJRvR6xMRXtA&r=nKjmvhl-FVZt-1iD5Ea7mULt--dNphU4qGuhbCJsO90&m=phs2270IlMJnrkY8KPgf3575O_9O78a6bb347nW4R28oRm_nFrGGqsw3BpqiTWZ4&s=hynNTB7tJ605xXLfaD7lhvL1xAyZ7JlB7RRwzxtQq0I&e=.)
> Unsubscribe & other changes: https://urldefense.proofpoint.com/v2/url?u=http-3A__lists.boost.org_mailman_listinfo.cgi_boost&d=DwICAg&c=9mghv0deYPYDGP-W745IEdQLV1kHpn4XJRvR6xMRXtA&r=nKjmvhl-FVZt-1iD5Ea7mULt--dNphU4qGuhbCJsO90&m=phs2270IlMJnrkY8KPgf3575O_9O78a6bb347nW4R28oRm_nFrGGqsw3BpqiTWZ4&s=PmpOkIwy-jd3O0wS2fcKgI6GRM5c7a6g75F3Fo_jPhA&e=





_______________________________________________

Unsubscribe & other changes: https://urldefense.proofpoint.com/v2/url?u=http-3A__lists.boost.org_mailman_listinfo.cgi_boost&d=DwICAg&c=9mghv0deYPYDGP-W745IEdQLV1kHpn4XJRvR6xMRXtA&r=nKjmvhl-FVZt-1iD5Ea7mULt--dNphU4qGuhbCJsO90&m=phs2270IlMJnrkY8KPgf3575O_9O78a6bb347nW4R28oRm_nFrGGqsw3BpqiTWZ4&s=PmpOkIwy-jd3O0wS2fcKgI6GRM5c7a6g75F3Fo_jPhA&e=

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.

_______________________________________________

Reply all
Reply to author
Forward
0 new messages