Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
if I want to return multiple values in c# I will return for example

c#

(string name, string jobTitle) GetEmployee()
{
...
}

in c++ since you can only see the header it looks something like this

///<returns> name, jobTitle
std :: tuple<string, string=""> GetEmployee();


Is there a way to return multiple values in c++ with names like in c#
so I don't have to put a <returns> tag on every method

What I have tried:

For now I return data in a std :: tuple
Posted
Updated 13-Dec-20 14:09pm

You can keep your tuple as it is.
C++
std::tuple<string, string> GetEmployee();
C++17 has structured binding which allows for returning multiple values from std::pair or std::tuple.

How to return multiple values from a function in C++17[^]

You can define the returned variables in this way when calling GetEmployee()
C++
auto [name, jobTitle] = GetEmployee();
To use structured binding in Visual C++ 2019, you have to set the language to C++17 by right-click on the project in the Solution Explorer to click on Properties on the popup menu and on the General tab, change the C++ Language Standard from Default(ISO C++14 Standard) to Default ISO C++ 17 Standard (/std:c++17). Click Ok to close the Properties dialog.
 
Share this answer
 
Comments
CPallini 14-Dec-20 2:29am    
5.
Shao Voon Wong 14-Dec-20 2:42am    
Thanks!
You can pass references to the items you want to set. Something like,
bool GetEmployee( std::string & name, std::string & title );
then you set those values in the function. You could return true if the values are found and set, false if not.
 
Share this answer
 
What you refer to is just documentation which explains what the function returns. It has no meaning to the compiler. See std::tuple - cppreference.com[^] for examples.
 
Share this answer
 
v4

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900