Returning more than one value from a function

M

Mohitz

is pair<> fine for returning two values?
What about more than two values?

PS: Values may be of different types.

Opinions?

Thank you
Mohit
 
M

Mohitz

I like lists alot...

Heterogenous Lists?? I thought STL lists were meant for data of one
type..

Mohit
 
E

Emery

is pair<> fine for returning two values?
What about more than two values?

PS: Values may be of different types.

Opinions?

Thank you
Mohit

You know you can return a struct? I think the uses of std::pair even
in the STL should have been structs. std::map for example uses a pair
for items where .first and .second hold the key and value
respectively. You have to know this ahead of time, where as if an item
struct with .key and .value members was used you would not need to
refer to the documentation. The only "appropriate" use for a generic
pair container IMO would be within another container that takes two
arbitrary types that are only known to be coupled together. But that's
just my personal opinion.
 
M

Mohitz

You know you can return a struct? I think the uses of std::pair even

Let's assume hypothetically that i write 100 functions in my program
and 50 of them
need to return more than 1 value. And let's assume that every function
needs a diff
struct. Am i supposed to write 50 different structs for that? Just
doesn't seem to
be the correct way!
 
A

Alan Woodland

Mohitz said:
Let's assume hypothetically that i write 100 functions in my program
and 50 of them
need to return more than 1 value. And let's assume that every function
needs a diff
struct. Am i supposed to write 50 different structs for that? Just
doesn't seem to
be the correct way!
Your functions could always be modified to take references e.g:

std::pair<int,int> foo() {
return std::make_pair(1,3);
}

void foo(int& a, int& b) {
a = 1;
b = 3;
}

Can be viewed as achieving similar behaviour to returning two ints in a
pair.

Alan
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

You know you can return a struct? I think the uses of std::pair even
in the STL should have been structs. std::map for example uses a pair
for items where .first and .second hold the key and value
respectively. You have to know this ahead of time, where as if an item
struct with .key and .value members was used you would not need to
refer to the documentation. The only "appropriate" use for a generic
pair container IMO would be within another container that takes two
arbitrary types that are only known to be coupled together. But that's
just my personal opinion.

A pair is just a parameterised struct, the only difference between what
you proposed and using pair is that pair is more generic than specific
structs. Besides, once you've used them you have no problem remembering
that it's first and second and not key and value.
 
J

James Kanze

Let's assume hypothetically that i write 100 functions in my program
and 50 of them
need to return more than 1 value. And let's assume that every function
needs a diff
struct. Am i supposed to write 50 different structs for that? Just
doesn't seem to
be the correct way!

Au contraire. It seems to be the only correct way. If two
functions have different return types, then it is normal that
you have to define two different types.

I can't really think of a single case where using std::pair
would be appropriate, except for emulating certain design errors
in the standard library. (If you're writing a container class,
it's best that it look as much as possible like a standard
container. Ditto for basic algorithms. So I use std::pair in
those.)
 
M

Mohitz

I can't really think of a single case where using std::pair
would be appropriate, except for emulating certain design errors
in the standard library. (If you're writing a container class,
it's best that it look as much as possible like a standard
container. Ditto for basic algorithms. So I use std::pair in
those.)

What is the harm in using pair<> to return two values from a function?
Saves the effort of defining a new struct.

Mohit
 
J

James Kanze

What is the harm in using pair<> to return two values from a function?

Readability. Unless the semantic of the values really is
"first" and "second", of course---pair<int,int> might be
appropriate for a function "throwTwoDice()". If you consider
something like std::map<>::value_type, however, the semantic
isn't first and second, but key and value, and using std::pair
for this is extremely bad design.
Saves the effort of defining a new struct.

Boy are you lazy:). Seriously, what's the big deal about a new
struct, if it makes the code clearer?
 
D

Diego Martins

Readability. Unless the semantic of the values really is
"first" and "second", of course---pair<int,int> might be
appropriate for a function "throwTwoDice()". If you consider
something like std::map<>::value_type, however, the semantic
isn't first and second, but key and value, and using std::pair
for this is extremely bad design.


Boy are you lazy:). Seriously, what's the big deal about a new
struct, if it makes the code clearer?

I can realize now the reinterpret_cast<> huge keyword size was
intentional to discourage lazy coders :)

Diego
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top