thecoder
thecoder

Reputation: 7

Error with Auto variable in a function C++

I'm having problems on passing a value between functions in C++. I add the code below. In mqttReceive, a MQTT message in JSON is received and it is sent again in send() to be received in void send(). However, I've tried to declare the received message as auto, but it doesn't work. What I'm missing?

cpp:

void MqttApplication::mqttReceive()
{
    try {
        
        mqttClient->start_consuming();
        mqttClient->subscribe(TOPIC, QOS)->wait();
        
    }
    catch (const mqtt::exception& exc) {
        cerr << exc.what() << endl;
        return;
    }

    
    while (true) {
        auto msg = mqttClient->consume_message();   
        

        try {               
            send(msg);
        }
        catch (const mqtt::exception& exc) {
            cerr << exc.what() << endl;
            return;
        }

        if (msg->get_topic() == "command" &&
                msg->to_string() == "exit") {
            cout << "Exit command received" << endl;
            break;
        }

        cout << msg->get_topic() << ": " << msg->to_string() << endl;
    }
}



void MqttApplication::send(auto msg)
{
    ...
}

hpp:

class MqttApplication : public Application
    {
    private:    
    
        void send(const auto msg);
    
        void mqttReceive();
        

The error:

In file included from /home/mqtt_application.cpp:1:
/home/mqtt_application.hpp:26:24: warning: use of ‘auto’ in parameter declaration only available with ‘-fconcepts-ts’
   26 |     void send(const auto& msg) override;
      |                        ^~~~
/home/mqtt_application.hpp:26:35: error: member template ‘void MqttApplication::send(const auto:1&)’ may not have virt-specifiers
   26 |     void send(const auto& msg) override;
      |                                   ^~~~~~~~
/home/mqtt_application.cpp:320:34: warning: use of ‘auto’ in parameter declaration only available with ‘-fconcepts-ts’
  320 | void MqttApplication::send(auto& msg)
      |                                  ^~~~
/home/mqtt_application.cpp:320:6: error: no declaration matches ‘void MqttApplication::send(auto:2&)’
  320 | void MqttApplication::send(auto& msg)
      |      ^~~~~~~~~~~~~~~~~~
In file included from /home/mqtt_application.cpp:1:
/home/mqtt_application.hpp:26:10: note: candidate is: ‘template<class auto:1> void MqttApplication::send(const auto:1&)’
   26 |     void send(const auto& msg) override;
      |          ^~~~~~~
In file included from /home/mqtt_application.cpp:1:
/home/mqtt_application.hpp:15:7: note: ‘class MqttApplication’ defined here
   15 | class MqttApplication : public Application
      |       ^~~~~~~~~~~~~~~~~~
tools/mqtt.dir/build.make:62: recipe for target 'tools/mqtt.dir/mqtt_application.cpp.o' failed
make[2]: *** [tools/mqtt.dir/mqtt_application.cpp.o] Error 1
CMakeFiles/Makefile2:834: recipe for target 'tools/mqtt.dir/all' failed
make[1]: *** [tools/mqtt.dir/all] Error 2
Makefile:129: recipe for target 'all' failed
make: *** [all] Error 2

I'm compiling with C++14. I've tried all kind of configurations, string, int, etc. The input is a regular JSON string. Thanks

Upvotes: 1

Views: 863

Answers (3)

Nikos C.
Nikos C.

Reputation: 51930

This syntax for function templates requires C++20. For C++17 and older, you need to change this:

void MqttApplication::send(auto msg)

to:

template <typename T>
void MqttApplication::send(T msg)

Both are equivalent, but the C++20 version is shorter. See Abbreviated function template.

Upvotes: 2

Surt
Surt

Reputation: 16129

Using auto as a parameter is a C++20 feature, some compilers support it as extensions in earlier versions but its not ISO there.

Upvotes: 2

gnasher729
gnasher729

Reputation: 52632

I wonder what you expected to happen? With "auto" in a function declaration, the compiler cannot know what the type is so it refuses to compile. That's different from "auto x = 3; " where the compiler knows the type of x should be the same as the type of 3.

If your compiler understands concepts and you understand concepts then you can make your code build, but that requires you understanding a major brand new C++ feature.

Upvotes: 0

Related Questions