I'm developing a template library for IPC in my application using Boost.Interprocess as base dependency.
I'm getting the bellow mentioned error.
I intend to mainly struct (by type punning) and some-times classes (by binary serialization).
I'm passing const reference as a parameter.
I tried removing const specifier using const_cast type conversion before converting it to
Error
1>C:\Users\admin\source\repos\TestApplication\TestApplication\Ipc.h(49,1): error C2440: 'reinterpret_cast': cannot convert from 'T' to 'void *'
1>C:\Users\admin\source\repos\TestApplication\TestApplication\Ipc.h(53,1): error C2440: 'reinterpret_cast': cannot convert from 'const T' to 'void *'
Code
template <typename T> class Ipc {
public:
bool send(const T& buffer, std::uint32_t priority = 0);
Ipc(const std::string& queue_name, bool communication_module = false, int message_max_number = 5) : mq_com_receive(boost::interprocess::open_or_create, (queue_name + "_receive").c_str(), message_max_number, sizeof(T)), mq_com_send(boost::interprocess::open_or_create, (queue_name + "_send").c_str(), message_max_number, sizeof(T)),
communication_module(communication_module) {}
private:
boost::interprocess::message_queue mq_com_receive;
boost::interprocess::message_queue mq_com_send;
bool communication_module; };
template<typename T>
inline bool Ipc<T>::send(const T& buffer, std::uint32_t priority)
{
bool communication_module = true;
try
{
if (communication_module == true)
{
return this->mq_com_send.try_send(reinterpret_cast<void*>(const_cast<T>(buffer)), sizeof(buffer), priority);
}
else
{
return this->mq_com_receive.try_send(reinterpret_cast<void*>(buffer), sizeof(buffer), priority);
}
}
catch(const std::exception& ex)
{
//TODO Log Exception
return false;
}
}
main.cpp
typedef struct
{
std::uint8_t id;
std::string ip;
}sensor_data_t;
int main()
{
sensor_data_t data;
data.id = 1;
data.ip = "192.168.1.101";
Ipc<sensor_data_t> i{ "test" };
i.send(data);
}