The function is called get_topic_names_and_types
`. For a more in depth answer go to this almost duplicated question
EDIT:
You'll need also to spin briefly to let the discovery happen and see the topics. For example the code could look like
rclcpp::Rate r(100);
int i = 0;
while (rclcpp::ok() && i < 100) {
rclcpp::spin_some(this->get_node_base_interface());
r.sleep();
i++;
}
// Now you can call the function to get topics
std::map<std::string, std::vector<std::string>> topic_infos = this->get_topic_names_and_types();
for (const auto& topic_it : topic_infos) {
std::string topic_name = topic_it.first;
std::vector<std::string> topic_types = topic_it.second;
// iterate over all topic types
for (const auto& topic_type : topic_types)
RCLCPP_INFO(this->get_logger(), "Topic: %s, type: %s", topic_name.c_str(), topic_type.c_str());
}