Monday, April 27, 2009

comp.lang.c++ - 25 new messages in 11 topics - digest

comp.lang.c++
http://groups.google.com/group/comp.lang.c++?hl=en

comp.lang.c++@googlegroups.com

Today's topics:

* Polymorphism at run-time - 5 messages, 4 authors
http://groups.google.com/group/comp.lang.c++/t/afb5fdfa6ae286c5?hl=en
* std::abs ambiguity - 5 messages, 4 authors
http://groups.google.com/group/comp.lang.c++/t/ecfa9110b83039de?hl=en
* Answer my queation - 2 messages, 2 authors
http://groups.google.com/group/comp.lang.c++/t/ff10a5fbe8340241?hl=en
* Free shipping wholesale Nike Gucci Dior POLO LV D&G Lacoste BOSS shoes ,
handbags,Clothing,shirt,glass ,PayPal from china - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/112c7632d8bc89ec?hl=en
* efficient priority queue for a few descrete priority levels - 2 messages, 1
author
http://groups.google.com/group/comp.lang.c++/t/841a3f2a5510ce4d?hl=en
* Eclipse - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/deaa133771727c03?hl=en
* hi - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/f25e07f690934489?hl=en
* C++ is complicated - 2 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/ec94a39f632a6c05?hl=en
* Optimizer Behavior with a Comparison - 4 messages, 4 authors
http://groups.google.com/group/comp.lang.c++/t/7e633f44fe066f5f?hl=en
* we can supply varied styles and colors shoes,clothing,bags ect. - 1 messages,
1 author
http://groups.google.com/group/comp.lang.c++/t/8ad568fbb703a2e7?hl=en
* Data Types Problem - 1 messages, 1 author
http://groups.google.com/group/comp.lang.c++/t/95a9b0dedc0f9c93?hl=en

==============================================================================
TOPIC: Polymorphism at run-time
http://groups.google.com/group/comp.lang.c++/t/afb5fdfa6ae286c5?hl=en
==============================================================================

== 1 of 5 ==
Date: Mon, Apr 27 2009 6:13 am
From: Jeff Schwab


tommo97 wrote:

> if I have a base class is it possible to recursively create new
> derived classes from it "on the fly" at runtime?

No. Classes in C++ can be defined only at compile-time.

> What I mean is that
> each derived class will have it's own static members which can be
> calculated from the base class using various functions,

Possible at compile-time through template metaprogramming.

> but that the
> number and variation between the derived class and the base class may
> (are) not known at compile time.

No can do, unless you're willing to generate and compile a separate
program, on the fly, at run-time. There are programs that do so with
spectacular success (e.g. FFTW), but it's probably not what you need.

> For example an octree:
> Base class is a node;
> 1st derived class is a root node, the next is derived from this;
> 2nd - nth derived classes are nodes at the various levels of the tree
> - these are derived from one-another;
> (n+1)th derived class is the leaf and is derived from the previous
> class.

You want objects. C++ classes are not objects. Unless you're
metaprogramming, it does not make much sense for a class to be a "node"
of any kind. I see why you would think this made sense, since a class
hierarchy does form a sort of tree; however, it's a very specialized
kind of tree, not a general-purpose data structure.

What you probably want is a general-purpose node class template.
Instantiate the template to get a class, and instantiate the class to
get an object (a "node").

template<class Value_type>
class node
{
typedef node* node_pointer;

Value_type m_value;
std::vector<node_pointer> m_children;
public:

node( Value_type value )
: m_value( value ) { }

// ...
};


== 2 of 5 ==
Date: Mon, Apr 27 2009 6:41 am
From: Phlip


tommo97 wrote:

> The reason I am interested in each level of the tree having thier own
> derived class is that I would like each derived class to "own" a
> static instance of a (large) set of data common to all nodes at that
> level, but unique to that level (in fact, if this is/were possible I
> would go one further and have data unique to another derived class
> representing each octant on each level). The idea is that this data,
> being large, takes a while to generate and I have no desire to
> duplicate it.

Firstly, El Goog returns 2 100 hits for [ogre3d octree], so see what they did.

Secord, when you say "polymorphism" and "derived class", there's no reason to
derive without changing behavior. Yet each octree node has exactly the same
behavior, just different data. Ordinary C++ data structures will handle this
situation easily...


== 3 of 5 ==
Date: Mon, Apr 27 2009 7:52 am
From: pjb@informatimago.com (Pascal J. Bourguignon)


tommo97 <tommo97@gmail.com> writes:

> Dear all,
>
> I am a relative novice to C++ and was wondering:
>
> if I have a base class is it possible to recursively create new
> derived classes from it "on the fly" at runtime? What I mean is that
> each derived class will have it's own static members which can be
> calculated from the base class using various functions, but that the
> number and variation between the derived class and the base class may
> (are) not known at compile time.
>
> For example an octree:
> Base class is a node;
> 1st derived class is a root node, the next is derived from this;
> 2nd - nth derived classes are nodes at the various levels of the tree
> - these are derived from one-another;
> (n+1)th derived class is the leaf and is derived from the previous
> class.
>
> Sorry if this makes no sense. If you have any idea even of the correct
> posing of this question please let me know!

C++ doesn't allow you to do anything at run-time. If you really
needed to create classes at run-time, you would have to use a more
powerful programming language, such as Common Lisp.

However, you haven't mentionned an example that would need run-time
class creation. If you just have node objects with variable fields,
you can just store these fields in a dictionnary (eg. a stl::map),
that would be a member of the same Node class.

--
__Pascal Bourguignon__


== 4 of 5 ==
Date: Mon, Apr 27 2009 8:50 am
From: tommo97


On Apr 27, 3:52 pm, p...@informatimago.com (Pascal J. Bourguignon)
wrote:
> tommo97 <tomm...@gmail.com> writes:
> > Dear all,
>
> > I am a relative novice to C++ and was wondering:
>
> > if I have a base class is it possible to recursively create new
> > derived classes from it "on the fly" at runtime? What I mean is that
> > each derived class will have it's own static members which can be
> > calculated from the base class using various functions, but that the
> > number and variation between the derived class and the base class may
> > (are) not known at compile time.
>
> > For example an octree:
> > Base class is a node;
> > 1st derived class is a root node, the next is derived from this;
> > 2nd - nth derived classes are nodes at the various levels of the tree
> > - these are derived from one-another;
> > (n+1)th derived class is the leaf and is derived from the previous
> > class.
>
> > Sorry if this makes no sense. If you have any idea even of the correct
> > posing of this question please let me know!
>
> C++ doesn't allow you to do anything at run-time.  If you really
> needed to create classes at run-time, you would have to use a more
> powerful programming language, such as Common Lisp.
>
> However, you haven't mentionned an example that would need run-time
> class creation.  If you just have node objects with variable fields,
> you can just store these fields in a dictionnary (eg. a stl::map),
> that would be a member of the same Node class.
>
> --
> __Pascal Bourguignon__

Dear all,
Thanks for your input. I will address some of your points in order,
and maybe (hopefully) clarify what I'm trying to achieve.
Jeff (and Philip): I have already got a fully working octree
implementation, with root, branches and leaves. This was hand crafted
(well bodged) by me a few years ago, and is starting to creak a little
in current use (which is as a structure for fast-multipole n-body
problems). The idea of using templates is something I will have a go
at tonight.

Philip: The behaviour with each node (in this case) is strongly
dependant on the level within the tree - ie at different levels there
are different forces (literally) at work. Therefore, it has not been
possible to just pick an octree off the shelf plus in this case speed
is fairly essential so things I need the tree to do are not what
people would generally use octrees for when ray-tracing, for example.

Pascal: At compile time and indeed at run time the user (er.. me) is
not aware of the number of recursions required within the tree, and
therefore the number of combinations of different particle-particle
interaction that may occur. Since the distance cutoff, strenght and
effect of, say, electrostatic potential, gravity and van der Waals
forces are so different, the properties of the node, given it's
location in the tree, must reflect the forces that it's contents have
on it's neighbours. For some reason - probably my ignorance - I
thought there would be some way that instances of the class could be
related in the sense that they are all parts of the same tree, but
different in the sense that a call to a particular member function at
a level would run an appropriate function with the relevent set of
parameters.

All in all, I'm trying to make as much information as possible
implicit within the tree, generally to avoid mistakes on my part, and
specifically to avoid memory issues.

Thanks again for your comments

Tm

== 5 of 5 ==
Date: Mon, Apr 27 2009 9:55 am
From: "Phlip"


> Thanks for your input. I will address some of your points in order, and
> maybe (hopefully) clarify what I'm trying to achieve. Jeff (and Philip): I
> have already got a fully working octree implementation, with root,
> branches and leaves. This was hand crafted (well bodged) by me a few years
> ago, and is starting to creak a little in current use (which is as a
> structure for fast-multipole n-body problems).

If it has no unit tests, add them now. Then refactor, one small edit at a
time, passing all the tests after each edit.

Keep refactoring until the code is DRY - that means it obeys "Don't Repeat
Yourself".

Seriously - when it's DRY, whatever design you get, it's good.

> Philip: The behaviour with each node (in this case) is strongly dependant
> on the level within the tree - ie at different levels there are different
> forces (literally) at work.

This might require the Strategy Pattern, to make the behavior itself
pluggable. How's your /Design Patterns/ foo?


==============================================================================
TOPIC: std::abs ambiguity
http://groups.google.com/group/comp.lang.c++/t/ecfa9110b83039de?hl=en
==============================================================================

== 1 of 5 ==
Date: Mon, Apr 27 2009 6:41 am
From: Kaba


Hi,

Anyone knows why the 'abs' function is ambiguous here? Tried to compile
with both Visual Studio 2008 Sp1 and Comeau. Ignore the fact that
Own::abs does not do the right thing.

The way I see it, the Standard Library abs should be in namespace std,
the Koenig lookup shouldn't activate for native types, and thus there
should be no ambiguity.

#include <cmath>

namespace Own
{

float abs(float x)
{
return x;
}

}

using namespace Own;

int main()
{
float a = 5;
float b = 3;
float c = abs(a - b);

return 0;
}


--
http://kaba.hilvi.org


== 2 of 5 ==
Date: Mon, Apr 27 2009 6:53 am
From: red floyd


Kaba wrote:
> Hi,
>
> Anyone knows why the 'abs' function is ambiguous here? Tried to compile
> with both Visual Studio 2008 Sp1 and Comeau. Ignore the fact that
> Own::abs does not do the right thing.
>
> The way I see it, the Standard Library abs should be in namespace std,
> the Koenig lookup shouldn't activate for native types, and thus there
> should be no ambiguity.
>
> #include <cmath>
>
> namespace Own
> {
>
> float abs(float x)
> {
> return x;
> }
>
> }
>
> using namespace Own;
>
> int main()
> {
> float a = 5;
> float b = 3;
> float c = abs(a - b);
>
> return 0;
> }

Most implementations also put the Standard C library routines into the
global namespace as well.

== 3 of 5 ==
Date: Mon, Apr 27 2009 7:30 am
From: Kaba


red floyd wrote:
> Most implementations also put the Standard C library routines into the
> global namespace as well.

Thanks. Better to avoid using names from Standard Library then...

--
http://kaba.hilvi.org


== 4 of 5 ==
Date: Mon, Apr 27 2009 8:41 am
From: "Bo Persson"


Kaba wrote:
> red floyd wrote:
>> Most implementations also put the Standard C library routines into
>> the global namespace as well.
>
> Thanks. Better to avoid using names from Standard Library then...

Or perhaps better avoid using "using namespace xxx"?


Bo Persson


== 5 of 5 ==
Date: Mon, Apr 27 2009 10:38 am
From: joshuamaurice@gmail.com


On Apr 27, 8:41 am, "Bo Persson" <b...@gmb.dk> wrote:
> Kaba wrote:
> > red floyd wrote:
> >> Most implementations also put the Standard C library routines into
> >> the global namespace as well.
>
> > Thanks. Better to avoid using names from Standard Library then...
>
> Or perhaps better avoid using "using namespace xxx"?

Both are probably good ideas considering he'd hit the same problem if
the call to abs was in the same namespace as his abs function.

==============================================================================
TOPIC: Answer my queation
http://groups.google.com/group/comp.lang.c++/t/ff10a5fbe8340241?hl=en
==============================================================================

== 1 of 2 ==
Date: Mon, Apr 27 2009 6:51 am
From: red floyd


shahanshah.ali@gmail.com wrote:
> WHAT DO U MEAN BY C++,

What if I don't answer your queation?


== 2 of 2 ==
Date: Mon, Apr 27 2009 8:02 am
From: Phlip


shahanshah.ali@gmail.com wrote:

> WHAT DO U MEAN BY C++,

C stands for Compiler, and C is an old compiled language popular as a kind of
"portable assembler".

++ is C's incrementation operator, so C++ is a software pun - the next increment
above C.

Now what will you do with this WikiPedia-level answer??

==============================================================================
TOPIC: Free shipping wholesale Nike Gucci Dior POLO LV D&G Lacoste BOSS shoes ,
handbags,Clothing,shirt,glass ,PayPal from china
http://groups.google.com/group/comp.lang.c++/t/112c7632d8bc89ec?hl=en
==============================================================================

== 1 of 1 ==
Date: Mon, Apr 27 2009 7:19 am
From: fjrjtrade005@tom.com


www.fjrjtrade.com Paypal payement ! Factory price! Free shipping!
2009 New NBA jersey, MLB Jersey
NFL jersey NHL jersey NBA jersey . Made-in-china Nike shoes ,
Puma shoes , Boss shoes
Adidas shoes Jordan shoes , Dior shoes ,A&F LV bags , Gucci bags ,
( www.fjrjtrade.com)

Paypal payement ! Factory price! Free shipping!
Our company hot sell on:

Brand shoes: ( www.fjrjtrade.com)
Air Force one ,Nike Air Jordan, Nike AirMax, Nike Shox ,Adidas
Shoes,UGG Boot Shoes,Puma Shoes
Bape Shoes ,Timberland Shoes , 4US Shoes Amauri Shoes ,Bikkembergs
Shoes, BOSS Shoes,Burberry Shoes
Lacoste shoes , Chanel Shoes,Coach Shoes ,D&G Shoes ,Diesel
Shoes ,Dior Shoes ,DSQUARED Shoes
ED Hardy Shoes , Evisu Shoes,Fendi Shoes , FootBall Shoes ,Hogan
Shoes ,Levis Shoes,Gucci Shoes,LV Shoes ,
RICH Shoes , SEBAGO Shoes , Versace Shoes , Y3 Shoes, Prada Shoes

Wholesale 2009 new All- MLB Jersey NF footballjersey NHL hockey
jersey ,NBA basketballjersey,MLB bassball Jersey .

Brand handbags:( www.fjrjtrade.com)

Balenciaga Handbags , Burberrys Handbag , Chanel Handbag , Chloe
Handbag , Christian Audigier Handbags , Coac Handbag
D&B Handbag , D&G Handbag Ed Hardy Handbag , Fendi Handbag , Gucci
Handbag , GUSS Handbags , Hermas Handbag
Jimmy Hoo Handbag , Juicy Handbag , LV Handbag , Miu Miu Handbags
Prada Handbag UGG Handbag

Brand Clothing: ( www.fjrjtrade.com)

Adidas , Man Short A&F, AFF,ARMANI Long BAPE , Man Short BBC , Short
Burberry , Short CA , COOGI, Man Short DG ,
Diesel , Short ECKO , ED Hardy , EVISU , Short GGG , Short G-STAR ,
Lacoste , Long LEVI,S , Man Short LRG, Man Short POLO ,
PRADA , Man Short Rocawear , SINFUL , Women Short SMET ,Man Short
VERSACE ,Man Short COACH Women ,

Brand Jeans Jacket Coat Watch to check ( www.fjrjtrade.com)

Top Quality , 5-7 days shipping time. Factory price

Contact:susan
msn:fjrjtrade@hotmail.com
website:www.fjrjtrade.com


==============================================================================
TOPIC: efficient priority queue for a few descrete priority levels
http://groups.google.com/group/comp.lang.c++/t/841a3f2a5510ce4d?hl=en
==============================================================================

== 1 of 2 ==
Date: Mon, Apr 27 2009 8:10 am
From: Marcel Müller


Hi,

Sam wrote:
> Given that your number of priorities is limited, all you need is a
> std::list for each individual priority.
>
> An object's priority selects its std::list. A push and a pop on a
> std::list is O(1).

oops, I think was too fixed on a single list with uncommitted items
still in the list and did not see the obvious solution.
All I have to do is to adjust the reader interface in the way, that it
starts looking at the highest priority, and the writer in a way that it
also signals readers with a lower priority limit. I think even a single
linked list should be fine, since push_back, pop_front and push_front
(for rollback) are O(1) for simple Head/Next/Tail lists too. And if I
join the lists the reader does no longer need to search all higher
priority lists each time. It only has to check for the individual tail
pointer corresponding to it's priority level.
Thanks for pushing me back to the right way.


Marcel


== 2 of 2 ==
Date: Mon, Apr 27 2009 8:22 am
From: Marcel Müller


red floyd wrote:
> comp.algorithms?

You are right, I did not know of this group so far. However, Sam already
put me in the right direction this time.


Marcel

==============================================================================
TOPIC: Eclipse
http://groups.google.com/group/comp.lang.c++/t/deaa133771727c03?hl=en
==============================================================================

== 1 of 1 ==
Date: Mon, Apr 27 2009 8:20 am
From: Noah Roberts


namekuseijin wrote:
> Noah Roberts wrote:
>> Michael Sgier wrote:
>>> I said to post here because the eclipse forums are DEAD
>>
>> Yeah, but you might eventually get help there. You're unlikely to
>> find it here.
>
> Yeah, here you'll only find emacs/vi hackers or people interested in the
> theoretical aspects of the language.

Actually, I use eclipse at home and we tried to make the switch to
eclipse at work. Unfortunately it just plain didn't work well enough to
kill Visual Studio and support was only good for certain few modules,
not including the CDT itself.

==============================================================================
TOPIC: hi
http://groups.google.com/group/comp.lang.c++/t/f25e07f690934489?hl=en
==============================================================================

== 1 of 1 ==
Date: Mon, Apr 27 2009 8:42 am
From: cool


The hill resort of Kodaikanal is jam-packed with beautiful spots of
interest. The colonial India finds its fullest expression at the
Kodai Club which houses period fireplace, the Leigh Hunt prints, the
piano, the library stocked with books by Evelyn Waugh, Anthony
Trollope, and the bar with stuffed heads of tigers, bison, deer-horns,
and a picture of the Laughing Cavalier by Franz Hals, the restaurant
with old English plates, a glowing hearth-fire, and landscape prints.
Another example of colonial India is the 114-acres of Golf Links.

for more details ........
www.kodai.org.in
http://kodaikanal-hills.blogspot.com/
www.saivijay.webs.com

==============================================================================
TOPIC: C++ is complicated
http://groups.google.com/group/comp.lang.c++/t/ec94a39f632a6c05?hl=en
==============================================================================

== 1 of 2 ==
Date: Mon, Apr 27 2009 9:17 am
From: Jerry Coffin


In article <6ODIl.6099$Lr6.2582@flpi143.ffdc.sbc.com>, tony@my.net
says...
> Jerry Coffin wrote:
> > In article <49c51f63$0$2727$ba4acef3@news.orange.fr>, jacob@nospam.org
> > says...
> >
> > [ ... ]
> >
> >> If I needed some "concept" I would create an abstract class
> >> that has all those properties and the compiler could check
> >> that the given type conforms to all the properties of the
> >> specified class.
> >
> > Not so. If you use inheritance, you quickly end up with something
> > virtually indistinguishable from Smalltalk. In particular, your type
> > checking ends being done at run-time rather than at compile time.
> >
> > The problem is fairly simple: inheritance means selecting a type far
> > enough up the tree that it's an ancestor of EVERY type you might care
> > to work with. Unfortunately, that almost inevitably includes a lot of
> > other types you don't want to work with.
> >
> > You also end up really warping the inheritance tree to make this work.
> >
> > Consider for example, sorting collections of objects. We can compare
> > strings, so we want to support sorting collections of strings. We can
> > also compare integers, so we want to support sorting collections of
> > strings. To support that, we have to apply 'sort' to some common
> > ancestor of both 'string' and 'integer' -- so far so good.
>
> Sounds like the solution is to stick with passing in a comparison function
> pointer to the container since the alternatives become complex quickly.

This isn't really much of a solution. Just for one example, it's how
qsort (from the C standard library) works. Problems include:

1) slow (e.g. std::sort is often three times as fast)
2) not type safe (comparison function receives pointers to void...)
3) complex to use (at least in the case of qsort)

It may be possible to work around the third to some degree (e.g. using
template tricks to determine element size) but the first two are much
more difficult problems.

It's certainly possible to produce vaguely template-like capabilities
using, for one example, macros -- in fact, some of us have done exactly
that. Long ago, before templates were part of the language, there was a
header called generic.h that allowed a degree of generic programming
without templates. This certainly reduces the complexity of the language
-- but it drastically _increased_ the complexity of programs written in
that language. It was a lousy tradeoff then, and it wouldn't be any
better now.

--
Later,
Jerry.

The universe is a figment of its own imagination.


== 2 of 2 ==
Date: Mon, Apr 27 2009 9:51 am
From: Jerry Coffin


In article <6ODIl.6100$Lr6.1056@flpi143.ffdc.sbc.com>, tony@my.net
says...
> Jerry Coffin wrote:
>
> > Looking at it from a slightly different direction, programming
> > languages are following the same path that databases did decades ago.
> > Databases progressed from the hierarchical model to the network model
> > and finally to the relational model.
>
> "finally"??? Most recently the relational model has had some competition in
> "certain" domains where the complexity of the relational model is overkill:
> See,
> http://www.infoworld.com/d/data-management/slacker-databases-break-all-old-rules-599.

I've already seen it. At least to me, it appears that the author thinks
"relational" equates to "SQL". This isn't really true. While it's
certainly true that these depart from the SQL database in various ways,
they all still follow the relational model to a fairly large degree.

Other than that, it's generally true that the relational model is rather
a "middle of the road" solution. A more free-form database tends to give
more flexibility at the expense of computing efficiency. A more
restricted model (e.g. hierarchical) tends toward the opposite. Both
have their uses, but neither appears likely to really dominate any time
soon.

It is, however, true that "finally" was probably a poor choice of words
-- the relational model (or some imitation of it) has dominated database
development for four decades or so, but even a complete implementation
probably wouldn't be the final stage of database development. The trend
toward cheaper computing power favors flexibility over efficiency.

> > Single inheritance implements
> > the hiearachical model. Multiple inheritance implements the network
> > model. Concepts implement the relational model.
>
> The question becomes, "what is the *simple solution* that will surface after
> concepts?".

I doubt that question has much relevance. Even if you could figure out
what language features (and such) were going to be used, say, 50 years
from now, I doubt it'd be much use. Trying to use that future language
on current hardware would be a bit like trying to run a current C++
compiler on an Apple II+ with 32K of RAM and 140K floppy disks.

--
Later,
Jerry.

The universe is a figment of its own imagination.

==============================================================================
TOPIC: Optimizer Behavior with a Comparison
http://groups.google.com/group/comp.lang.c++/t/7e633f44fe066f5f?hl=en
==============================================================================

== 1 of 4 ==
Date: Mon, Apr 27 2009 9:45 am
From: Andrew Tomazos


Please consider the following code...

void on_equal_to();
void on_less_than();
void on_greater_than();

enum EComparisonResult
{
eEqualTo,
eLessThan,
eGreaterThan
};

inline EComparisonResult CompareIntegers(int a, int b)
{
if (a == b)
return eEqualTo;
else if (a < b)
return eLessThan;
else
return eGreaterThan;
}

void f1(int a, int b)
{
switch (CompareIntegers(a,b))
{
case eEqualTo: on_equal_to(); return;
case eLessThan: on_less_than(); return;
case eGreaterThan: on_greater_than(); return;
}
}

void f2(int a, int b)
{
if (a == b)
on_equal_to();
else if (a < b)
on_less_than();
else
on_greater_than();
}

Would you expect that the optimizer will produce code for f1 that has
equal performance to f2? Why or why not?

Thanks,
Andrew.


== 2 of 4 ==
Date: Mon, Apr 27 2009 9:57 am
From: Carl


Andrew Tomazos wrote:
> Please consider the following code...
>
> void on_equal_to();
> void on_less_than();
> void on_greater_than();
>
> enum EComparisonResult
> {
> eEqualTo,
> eLessThan,
> eGreaterThan
> };
>
> inline EComparisonResult CompareIntegers(int a, int b)
> {
> if (a == b)
> return eEqualTo;
> else if (a < b)
> return eLessThan;
> else
> return eGreaterThan;
> }
>
> void f1(int a, int b)
> {
> switch (CompareIntegers(a,b))
> {
> case eEqualTo: on_equal_to(); return;
> case eLessThan: on_less_than(); return;
> case eGreaterThan: on_greater_than(); return;
> }
> }
>
> void f2(int a, int b)
> {
> if (a == b)
> on_equal_to();
> else if (a < b)
> on_less_than();
> else
> on_greater_than();
> }
>
> Would you expect that the optimizer will produce code for f1 that has
> equal performance to f2? Why or why not?
>
> Thanks,
> Andrew.


Depends on how smart your compiler is. You could simply try to run each
function a million times or so and clock it.


== 3 of 4 ==
Date: Mon, Apr 27 2009 10:13 am
From: Eric Sosman


Andrew Tomazos wrote:
> Please consider the following code...
> [... see up-thread ...]
>
> Would you expect that the optimizer will produce code for f1 that has
> equal performance to f2?

No. Of course there's a possibility that f1() and f2() could
perform identically, but I wouldn't "expect" them to.

> Why or why not?

One glaring difference is that there are only three execution
paths in f2() while f1() has four (in C, anyhow -- I don't know the
rules for C++; note the cross-post). True, the fourth path in f1()
will never be executed. The compiler might notice that the fourth
path is dead code, but "expecting" it to do so seems too much.

--
Eric.Sosman@sun.com


== 4 of 4 ==
Date: Mon, Apr 27 2009 10:28 am
From: Preston


On 2009-04-27 10:45:16 -0600, Andrew Tomazos <andrew@tomazos.com> said:

> Please consider the following code...
>
> void on_equal_to();
> void on_less_than();
> void on_greater_than();
>
> enum EComparisonResult
> {
> eEqualTo,
> eLessThan,
> eGreaterThan
> };
>
> inline EComparisonResult CompareIntegers(int a, int b)
> {
> if (a == b)
> return eEqualTo;
> else if (a < b)
> return eLessThan;
> else
> return eGreaterThan;
> }
>
> void f1(int a, int b)
> {
> switch (CompareIntegers(a,b))
> {
> case eEqualTo: on_equal_to(); return;
> case eLessThan: on_less_than(); return;
> case eGreaterThan: on_greater_than(); return;
> }
> }
>
> void f2(int a, int b)
> {
> if (a == b)
> on_equal_to();
> else if (a < b)
> on_less_than();
> else
> on_greater_than();
> }
>
> Would you expect that the optimizer will produce code for f1 that has
> equal performance to f2? Why or why not?
>
> Thanks,
> Andrew.

Is this your homework or something?


==============================================================================
TOPIC: we can supply varied styles and colors shoes,clothing,bags ect.
http://groups.google.com/group/comp.lang.c++/t/8ad568fbb703a2e7?hl=en
==============================================================================

== 1 of 1 ==
Date: Mon, Apr 27 2009 10:57 am
From: mingming


all kinds of clothing , shoes , bags etc.

we are selling all kinds of clothing , shoes , caps jewelry,
sunglass and watch in low price(paypal payment)
If you are interested in our products, please contact directly with
us. our website: www.fashion009.com
MSN: howard009.live.com

==============================================================================
TOPIC: Data Types Problem
http://groups.google.com/group/comp.lang.c++/t/95a9b0dedc0f9c93?hl=en
==============================================================================

== 1 of 1 ==
Date: Mon, Apr 27 2009 11:14 am
From: Ben


Hey,

I can't figure out what's going on here. I'm trying have an unsigned
short pointer point to an array of chars, and I expect the numerical
value of the short to be the sum of the two next chars, where the
first one is left-shifted by 8 bits (see the code below). It seems
that the value is actually the sum where the SECOND char is left-
shifted by 8 bits, and I can't figure out why. Isn't the 'a' the most
significant byte in the value below? Can someone explain this to me?
Thanks.

#include <stdio.h>

int main(int argc, char *argv[] )
{ // main program

unsigned short
*lpw;
char
str[3] = "ab";

lpw = (unsigned short *)&str[0]; // make lpw point to the 'a' in str
printf("*lpw == %hu\n", *lpw ); // print out the value of *lpw, as an
unsigned short

// CORRECT ONE
printf("*lpw should be equal to: %d\n", 'a' + ('b' << 8) ); // print
out the equivalent value, by adding 'a' and 'b' shifted properly
// INCORRECT ONE
printf("*lpw should be equal to: %d\n", 'b' + ('a' << 8) ); // print
out the equivalent value, by adding 'a' and 'b' shifted properly

} // end main()


==============================================================================

You received this message because you are subscribed to the Google Groups "comp.lang.c++"
group.

To post to this group, visit http://groups.google.com/group/comp.lang.c++?hl=en

To unsubscribe from this group, send email to comp.lang.c+++unsubscribe@googlegroups.com

To change the way you get mail from this group, visit:
http://groups.google.com/group/comp.lang.c++/subscribe?hl=en

To report abuse, send email explaining the problem to abuse@googlegroups.com

==============================================================================
Google Groups: http://groups.google.com/?hl=en

No comments:

Post a Comment