One line solution for referencing unused parameters

I use a function that uses 2 reference parameters to return values, only one of this value interests me. Is there a way to do this in one line.

I will explain myself with code

Function code

void Limits (double& min, double &max) { min = MIN; max = MAX; } 

using function

 double min; double unused; Limits(min, unused); // using myDouble but not unused 

What I would like to write is something like

 double min; Limits(min, double()); 

I can not find a way without first declaring the object. Is there an elegant solution?

I can not change the function.

+8
source share
5 answers

I'm not sure why the problem with declaring an extra variable is the problem. Regardless if you cannot change the function, create a wrapper:

 void MyLimits(double& min) { double unused; Limits(min, unused); } 

Or you can do something funky like this:

 double min; Limits(min, *std::unique_ptr<double>(new double)); 
+4
source

The most obvious solution would be to use pointers:

 void Limits( double* min, double* max ) { if ( min != nullptr ) { *min = myMin; } if ( max != nullptr ) { *max = myMax; } } // ... double min; Limits( &min, nullptr ); 

Alternatively, you can simply define individual functions:

 void Limits( double& min, double& max ); void UpperLimit( double& max ); void LowerLimit( double& min ); 

EDIT:

Since the original poster has since indicated that it cannot change the function, it must wrap it with one that uses pointers:

 void myLimits( double* min, double* max ) { double ignore; Limits( min == nullptr ? ignore : *min, max == nullptr ? ignore : *max ); } 
+2
source

No, but you can have a macro that just does it for you. The following example shows how to do this.

 #define LIMITS(min) { double unused; Limits(min, unused); } ... // Later in your code: double min; LIMITS(min); 

Note that I added {} to my macro. This is done to create a local area for an unused variable. Then you can use the macro from the same function.

+1
source

What a null reference. So you are stuck in pointers.

 void Limits(double* min, double* max) { if (min) *min = this->min_; if (max) *max = this->max_; } int min; Limits(&min, NULL); 

I know that I know, you're going to say "Pointers are Bad!". Pointers are not bad, but if you really don't like it, I suggest looking into boost::optional . Personally, I feel like this unnecessarily adds code complexity.

+1
source

I think with C ++ 11 and its rvalue references you can implement the requested single line string. It reads as follows:

 template<class T> T& make_dummy_out(T&& t) { return t; } 

Then you can call the Limit function as follows:

 double min; Limits(min, make_dummy_out(double())); 

This is safe, because the lifetime of the created ad-hoc double() will be until the end of the statement, that is, after the Limits() call is completed.

Note that make_dummy_out() is basically the opposite of std::move() : while std::move() explicitly turns the lvalue link into an rvalue link, make_dummy_out() converts the rvalue link explicitly into an lvalue link. Of course, you need to declare the template function only once, and then use it to create dummy output parameters where you need it.

0
source

Source: https://habr.com/ru/post/1207141/


All Articles