wow, I have been programming like 15 years and never heard of this
.
learning new...
But are you guys using rather references or you normally use "pair" if you
need to do this? Can pair s type be a reference so that it does not copy
values?
This raises at least three questions:
1- Why are you worrying that the pair get copied?
(hint: premature optimisation)
2- Why do you think that a copy actually happens rather than being
optimised away by the compiler?
3- Why do you prefer using what is an unnatural and more verbose
syntax e.g.:
---------------------------------
// Header
void some_function(some_arg_type some_arg, int &a, int & b);
// Usage
int a = 0; // Watch out if you don't pre-initialise
int b = 0;
some_function(some_arg, a, b);
----------------------------------
Over what is a more natural, less verbose and probably less error prone.
----------------------------------
// Header
typedef std:
air< int, int > int_pair;
int_pair some_function ( some_arg_type some_arg );
// Usage
int_pair answer = some_function(some_arg);
-----------------------------------
Note that while it may seem that the 2 lines in the header are worse
than the 1 line for the first case, you need three line everytime you
use the function which makes it much worse overall.
Note that the second form scales nicely to 3, 4, 5, 100 results by
using a struct as a return type or a container. The first form will
and up worse and worse.
Also, as hinted, you really should pre-initialise the response data
holder with your proposed technique. Failure to do so might hide a
very subtle bug and even then, you are relying on magic values. But
the net effect is that you code will end up writing data to a and b
twice which depending on the type of a and b might make the first
method actually slower than the second.
Yannick