0

OS : Windows 10 x64
Build Tool : Visual Studio 2021
Language Standard : C++20
paho-mqttpp3 : 1.2.0
Package Manager : vcpkg

I am trying to build a mqtt::async_client Singleton class using paho-mqttpp3 verrsion 1.2.0

I am using Meyers' implementation of a Singleton for my MQTT Client. Reference : https://stackoverflow.com/a/17713799/6319901

I have 2 questions

  1. Is their any technique where by I can use std::optional (returning an reference to MqttClient object) to inform main/application program that their was an exception in MqttClient::get_instance method?
  2. If I can't use std::optional while returning a reference then what options are available with me?

I am getting the bellow mentioned error

C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\optional(61,29): error C2625: 'std::_Optional_destruct_base<_Ty,true>::_Value': illegal union member; type 'MqttClient &' is reference type
        with
        [
            _Ty=MqttClient &
        ]
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\optional(110): message : see reference to class template instantiation 'std::_Optional_destruct_base<_Ty,true>' being compiled
        with
        [
            _Ty=MqttClient &
        ]
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\xsmf_control.h(82): message : see reference to class template instantiation 'std::_Optional_construct_base<_Ty>' being compiled
        with
        [
            _Ty=MqttClient &
        ]
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\xsmf_control.h(120): message : see reference to class template instantiation 'std::_Deleted_copy_assign<std::_Optional_construct_base<_Ty>,_Ty>' being compiled
        with
        [
            _Ty=MqttClient &
        ]
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\optional(154): message : see reference to class template instantiation 'std::_Deleted_move_assign<std::_Optional_construct_base<_Ty>,_Ty>' being compiled
        with
        [
            _Ty=MqttClient &
        ]
C:\Users\IshaanKarnik\My Drive\KPEC\Projects\Firmware\Windows\Edge Gateway\Edge Gateway\MqttClient.cpp(240): message : see reference to class template instantiation 'std::optional<MqttClient &>' being compiled
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\optional(161,62): error C2338: T in optional<T> must meet the Cpp17Destructible requirements (N4828 [optional.optional]/3).
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\optional(336,1): error C2528: '->': pointer to reference is illegal
C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.30.30705\include\optional(342,1): error C2528: '->': pointer to reference is illegal

Header :

class MqttClient : public virtual mqtt::callback
{
private:
    std::string                         server_uri{ "" };
    std::string                         client_id{ "" };
    std::unique_ptr<mqtt::async_client> client;
    static void                         init_singleton(MqttClient& instance);
    void                                connected(const std::string& cause) override;
    void                                connection_lost(const std::string& cause) override;
    void                                delivery_complete(mqtt::delivery_token_ptr tok) override;
    void                                message_arrived(mqtt::const_message_ptr msg) override;
    void                                on_failure(const mqtt::token& tok) override;
    void                                on_success(const mqtt::token& tok) override;
    MqttClient() = default;
    ~MqttClient() = default;
public:

    static std::optional<MqttClient&>   get_instance(void);

    MqttClient(const MqttClient& obj) = delete;
    MqttClient(MqttClient&& obj) = delete;
    MqttClient& operator=(const MqttClient& obj) = delete;
    MqttClient& operator=(MqttClient&& obj) = delete;
};

Source:

std::optional<MqttClient&> MqttClient::get_instance(void)
{
    try
    {
        static MqttClient       instance;
        static std::once_flag   initialize_client_flag;
        std::call_once(initialize_client_flag, MqttClient::init_singleton, instance);
        return instance;
    }
    catch (const std::exception& ex) 
    {
        PLOG_FATAL << ex.what();
    }
    return std::nullopt;
}

void MqttClient::init_singleton(MqttClient& instance)
{
    try
    {
        instance.client = std::make_unique<mqtt::async_client>(instance.server_uri, instance.client_id, Common::directory_path);
    }
    catch (const std::exception& ex) 
    {
        PLOG_FATAL << ex.what();
        throw ex;
    }
}
Dark Sorrow
  • 1,303
  • 9
  • 25

0 Answers0