Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > question about A a = b? f1(x,y,z) : f2(o,p,q);

Reply
Thread Tools

question about A a = b? f1(x,y,z) : f2(o,p,q);

 
 
Juha Nieminen
Guest
Posts: n/a
 
      10-12-2012
Victor Bazarov <(E-Mail Removed)> wrote:
>> Sometimes it's better to use additional variables even though they might
>> not be strictly mandatory, in order to make the code clearer and easier
>> to read.

>
> In that case the names of the variables (F1 vs f1 and F2 vs f2) does not
> seem to present any advantage in terms of clarity. Again, that's just
> my opinion, since clarity is not a universal trait.


It was example code. The names are ostensibly not that non-telling in
the original code.
 
Reply With Quote
 
 
 
 
Pavel
Guest
Posts: n/a
 
      10-14-2012
胡宇光 wrote:
> Think about the following code:
>
> void func() {
> ...
> A a = b? f1(x,y,z):f2(o,p,q);
> ...
> }
>
> 1, class A doesn't not have default constructor.
>
> 2, x,y,z,o,p,q and the f1,f2 name are very long.
>
> How can I make the code shorter?
>
> If 1x is allowed, maybe I can write
>
> auto F1 = [&]() {return f1(x,y,z);}
> auto F2 = [&]() {return f2(o,p,q);}
> A a = b? F1():F2();
>
> If use c++ 03, how can I make the code shorter?
>

try (using RVO it should be equivalent):
namespace /* if not in the header */ {
A
createA(bool b, XP x, YP y, ZP z, OP o, PP p, QQ p) {
if (b)
return f1(x, y, z);
return f2(o, p, q);
}
}

void
func() {
...
A a(createA(b, x, y, z, o, p, q));
...
}

Above, [X-ZO-Q]P) are the names of the hand-selected best parameter type to pass
in x..q, respectively.

The savings are long f1, f2; also the parameter names in createA can be quite
short; also, it would be easier to debug f1 and f2 branches if you use a basic
debugger.

HTH
-Pavel

 
Reply With Quote
 
 
 
 
胡宇光
Guest
Posts: n/a
 
      10-15-2012
在 2012年10月11日星期四UTC+8下午11时57分17 ,Stefan van Kessel写道:
> On 2012-10-11 12:37 PM, 胡宇光 wrote:
>
> > Think about the following code:

>
> >

>
> > void func() {

>
> > ...

>
> > A a = b? f1(x,y,z):f2(o,p,q);

>
> > ...

>
> > }

>
> >

>
> > 1, class A doesn't not have default constructor.

>
> >

>
> > 2, x,y,z,o,p,q and the f1,f2 name are very long.

>
> >

>
> > How can I make the code shorter?

>
> >

>
> > If 1x is allowed, maybe I can write

>
> >

>
> > auto F1 = [&]() {return f1(x,y,z);}

>
> > auto F2 = [&]() {return f2(o,p,q);}

>
> > A a = b? F1():F2();

>
> >

>
> > If use c++ 03, how can I make the code shorter?

>
>
>
>
>
> I assume you just want the
>
> A a = b? f1(x,y,z):f2(o,p,q);
>
> statement to be shorter / more readable, not the entire code.
>
>
>
> If so, you could try the following:
>
>
>
> const A& a1 = f1(x,y,z);
>
> const A& a2 = f2(o,p,q);
>
> A a = b?a1:a2;
>
>
>
> If you haven't come across const references to temporaries before, you
>
> may want to take a look at
>
> http://herbsutter.com/2008/01/01/got...portant-const/


const& a1 = f1(x,y,z)
const& a2 = f2(o,p,q)
it's diff with the A a = b? f1():f2().
The original code just execute one function, not both.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
question row filter (more of sql query question) =?Utf-8?B?YW5kcmV3MDA3?= ASP .Net 2 10-06-2005 01:07 PM
Quick Question - Newby Question =?Utf-8?B?UnlhbiBTbWl0aA==?= ASP .Net 4 02-16-2005 11:59 AM
Question on Transcender Question :-) eddiec MCSE 6 05-20-2004 06:59 AM
Question re: features of the 831 router (also a 924 question) Wayne Cisco 0 03-02-2004 07:57 PM
Syntax Question - Novice Question sean ASP .Net 1 10-20-2003 12:18 PM



Advertisments