What is the gain of "inline"

D

Dik T. Winter

>
> I had that problem licked a decade ago, when I developed an assertion
> system against multiple side effects in a macro expansion, for the
> paranoid. This is a paper tiger.

I think not, but see below.
>
> This proliferation is not an adequate replacement for proper
> polymorphism, like what we have in the operators.

Yes, you get that if you can not define your own polymorphism. C does not
allow it and so we can not get it properly.
> Though it exorcises the demon of multiple evaluation, it has pitfalls
> of its own. If you change the types of some arguments, you have to edit
> all of the calls. If you don't edit some of them, they will compile
> anyway, and so you may be calling imax on a pair of doubles.

RIght. The same holds if you change the type of an argument of any procedure.
It may compile with or without warning and can give wrong results or not.
> This is like going back to the B language, which had separate operators
> for floating math.

Ah, back to operators, but in C they are polymorphic by definition. In other
languages also functions can be polymorphic.
>
> A macro that evaluates twice, but exhibits type polymoprhism, is a
> better alternative than a proliferation of different max functions,
> in spite of the multiple evaluation risk of the macro version.

You can state that as an absolute, but it makes no sense as such. What
about the 'abs' function (which is *not* a macro and does not exhibit
type polymorphism)?
> Let's see, remember one simple thing---don't place side effects in the
> argument---versus remember which member of proliferation of functions to
> use when, and keep revising the choice when the surrounding code
> changes. Hmm.

When you change types (especially arithmetic types) you have *always* to
consider such things. Consider:
int a = 1;
int b = 3;
float c = 3;
int d;
d = (a / c) * b;
Do you not expect to change the code when the type of c is changed from
float to int because c can have integral values only?

Also consider:
void dmaxv(double *a, double *b, double *res, int n) {
while(n-- > 0) *res++ = dmax(*a++, *b++);
}
well known idiom in some circles (so much so that it was a single instruction
on the CDC Cyber 205). You would prefer it as:
void dmaxv(double *a, double *b, double *res, int n) {
while(n-- > 0) {
*res = MAX(*a, *b);
res++; a++; b++;
}
?

(And, yes, the field I come from is numerical mathematics where polymorphism
is much less a requirement than single evalutation.)
 
R

Richard Bos

jacob navia said:
Tom St Denis a écrit :

Tom, you're an idiot.
Inline functions MUST be written in header files...

jacob, you're an idiot.

Two idiots disagreeing 100% with one another doesn't make either of them
any jot less an idiot.

Richard
 
R

Richard Bos

Willem said:
Your arguments apply equally to macro's,

I know why you spelled it that way, but in English it's spelt without
the apostrophe.

Yes, I used to get this wrong, too. Probably still do, on occasion.

Richard
 
J

jacob navia

Richard Bos a écrit :
Feel free to piss off to comp.lang.c++.

Richard

That is comic. In the company we were forced to rview
all the C++ code that used "max" since in C++ max
doesn't allow for ifferent arguments in its parameters
since it is a template

D:\lcc\mc77\test>type max.cpp
#include <stdio.h>
template <class T>
T max(const T& g, const T& d)
{
return ((g > d) ? g : d);
}

int main(void)
{
printf("%d\n",(int) max(6.4,2));
}

D:\lcc\mc77\test>cl max.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 15.00.21022.08 for x64
Copyright (C) Microsoft Corporation. All rights reserved.

max.cpp
max.cpp(10) : error C2782: 'T max(const T &,const T &)' : template parameter 'T'
is ambiguous
max.cpp(3) : see declaration of 'max'
could be 'int'
or 'double'

This is progress. We had a mx macro that worked with anything numeric.
Now we have a template that doesn't work even for those obvious cases as above!
 
J

jacob navia

Richard Bos a écrit :
jacob, you're an idiot.

Ahh, yes. Obvious. Sorry, I forgot that

:)

P.S. To be able to insult me, I have to give *some*
importance to what you say...
 
J

jacob navia

Kaz Kylheku a écrit :
So even if we use C++ and templates to make the perfect polymorphic
max, which evalautes arguments just once,

you can't even take the max of 6.5 and 2...

D:\lcc\mc77\test>type max.cpp
#include <stdio.h>
template <class T>
T max(const T& g, const T& d)
{
return ((g > d) ? g : d);
}

int main(void)
{
printf("%d\n",(int) max(2.4,2));
}

D:\lcc\mc77\test>cl max.cpp
Microsoft (R) C/C++ Optimizing Compiler Version 15.00.21022.08 for x64
Copyright (C) Microsoft Corporation. All rights reserved.

max.cpp
max.cpp(10) : error C2782: 'T max(const T &,const T &)' : template parameter 'T'
is ambiguous
max.cpp(3) : see declaration of 'max'
could be 'int'
or 'double'

We had a macro that worked. Now we have a template that doesn't.

GREAT!

That is progres surely
 
K

Kaz Kylheku

Richard Bos a écrit :

That is comic. In the company we were forced to rview
all the C++ code that used "max" since in C++ max
doesn't allow for ifferent arguments in its parameters
since it is a template

That comic limitation is that of standard std::max template function,
not a limitation of templates.

We can make a template max which has two different types
for its arguments, and a result type which depends on that
combination of types.

Maybe when std::max was designed, this was not possible.
To compute the result type, we have to use template classes with partial
specialization.

template <typename A, typename B>
result<A, B>::type max(const A &, const B &);

result is a set of specialized template classes such that, for instance

result<char, int>::type

is a typedef name resolving to ``int'': i.e. when we take compute the
max of an int and a char, the result will be converted to the wider type
int.

If you introduce your own numeric types, you do have to add a
specialization (possibly more than one) for the result<> template.

But then this works with all other templates that make use of result said:
This is progress. We had a mx macro that worked with anything numeric.
Now we have a template that doesn't work even for those obvious cases as above!

That was a temporary setback when templates were less powerful. The
lesson to be learned there is: don't cast your standard library in
stone, while your language lacks expressive power. :)
 
K

Kaz Kylheku

Kaz Kylheku a écrit :

you can't even take the max of 6.5 and 2...

D:\lcc\mc77\test>type max.cpp
#include <stdio.h>
template <class T>
T max(const T& g, const T& d)
{
return ((g > d) ? g : d);
}

That's with the simple-minded std::max.

With the kaz::max template, you can.
 
J

jacob navia

Kaz Kylheku a écrit :
That's with the simple-minded std::max.

With the kaz::max template, you can.

Sure sure.

Phase 1: A max macro worked well and never gave any problems.
But... working code must be changed obviously.
Phase 2: Replacement template is now in the standard library
so it is impossible to use the macro. But the new template
solution is unable to take max(6.5,2); GREAT!
Phase 3: To make a max template that works I should use kaz::max
that uses unknown techniques (the template technique
of the c++ wizards that wrote the standard library is
too low level for the herculean task of calculating
max(6.4,2); ).

This is really a digest of the history of C++.

Why make simple when you can do it complicated?
 
I

Ian Collins

jacob said:
Kaz Kylheku a écrit :

Sure sure.

Phase 1: A max macro worked well and never gave any problems.
But... working code must be changed obviously.

and still does, unless you try and use max as a function name, or pass
its address to another function, or pass it the result of function calls
or...
Phase 2: Replacement template is now in the standard library
so it is impossible to use the macro. But the new template
solution is unable to take max(6.5,2); GREAT!

While a pain, most uses of max will be on two instances of the same
type. Other's experiences may differ, but I can't see an instance in my
code where I've use max in C or C++ on different types.
Phase 3: To make a max template that works I should use kaz::max
that uses unknown techniques (the template technique
of the c++ wizards that wrote the standard library is
too low level for the herculean task of calculating
max(6.4,2); ).

Being a template, the implementation will be clear for all to see.
This is really a digest of the history of C++.

Why make simple when you can do it complicated?

Why drive a model T when you can have a modern car? A feature may
appear crazy in isolation, but come onto its own when used in
combination with others.
 
J

jacob navia

Ian Collins a écrit :
and still does, unless you try and use max as a function name, or pass
its address to another function, or pass it the result of function calls
or...

You can't use max as a function name. So what? You write a
"MyMax" function for a specific comparison if the need
would arise.
While a pain, most uses of max will be on two instances of the same
type. Other's experiences may differ, but I can't see an instance in my
code where I've use max in C or C++ on different types.

Of course you NEVER use two different types in your code.
If you would, it woudn't compile...

GREAT!

:)

Being a template, the implementation will be clear for all to see.

Being a macro, the implementation will be hidden you think?
Or what?
Why drive a model T when you can have a modern car? A feature may
appear crazy in isolation, but come onto its own when used in
combination with others.

Two philosophies here.

My family has an old fashioned car, (1998) that has NO SOFTWARE
in it. NOTHING.

My neighbours go to the garage very often, to get software updates
for their cars. They can only repair their cars at approved garages
that charge twice as much as independent ones. The software in their cars
can only be understood by special machines that are sold to a
selected few...

Yes, the trend to "better" cars is obvious. Auto makers are at last
in the position of eliminating all independent garages around.

Progress.

Went to the chinese shop and bought an alarm clock. It has no buttons
but a touch screen, full of incomprehensible symbols. To get into the
"settings" menu you have to touch a certain part of the screen, and put
the clock in a special spacial orientation since the menus
change when you turn sideways the clock. It has a thick manual
that I had to read each time I set the alarm since it was so
complicated I always forgot.

BUT:

It told you also the temperature of
the room and had a wheather channel for 5 days. besides it
could tell ou the day of the week, the month, etc. You could
change the display to analogic or digital, maybe it would make the
coffee I did not read all the thick manual.

One day I got so fed up I threw it to the garden over the window
and bought another. It had 2 buttons, one for setting the time and
another for setting the alarm. No menus, no sofwtare no nothing.

PHEW!
 
P

Phil Carmody

jacob navia said:
My family has an old fashioned car, (1998) that has NO SOFTWARE
in it. NOTHING.

And this demonstrates clearly how you remain so remarkably and
painfully ignorant about embedded programming, seemingly thinking
that the windows desktop you point and drool at is the be all and
end all of computing. I bet there are microcontrollers aplenty in
that car.

Phil
 
I

Ian Collins

jacob said:
Ian Collins a écrit :

You can't use max as a function name. So what? You write a
"MyMax" function for a specific comparison if the need
would arise.

How about the other exceptions I listed?
Of course you NEVER use two different types in your code.
If you would, it woudn't compile...

That's not what I said, please read with greater care.
Being a macro, the implementation will be hidden you think?
Or what?

Where did I say that? The visibility od templates and macros is the
same. You claimed Kaz's template "uses unknown techniques", which
clearly isn't the case.
Two philosophies here.

My family has an old fashioned car, (1998) that has NO SOFTWARE
in it. NOTHING.

Are you sure? I drive a 1995 (the latest that can safely be filled with
water) 4WD and it still has an ECU, and airbag system and ABS. I guess
there were 1998 cars with normally aspirated engines, no airbags, ABS or
even radios, but I for one would have avoided them.
 
K

Kaz Kylheku

While a pain, most uses of max will be on two instances of the same
type. Other's experiences may differ, but I can't see an instance in my
code where I've use max in C or C++ on different types.

size_t read_size = min(end - ptr, remaining);

remaining is unsigned, end - ptr is ptrdiff_t.
 
K

Kaz Kylheku

My family has an old fashioned car, (1998) that has NO SOFTWARE
in it. NOTHING.

I doubt it. The last vehicle produced that didn't have electronic fuel
injection was supposedly a 1990 Nissan Pathfinder.

What 1998 car doesn't have a computer that runs the engine and
diagnoses it?

If you mean that your 1998 car has no firmware that can be /upgraded/,
that's different. A write-only program is still code.

I do tend to agree that upgradable firmwares are bad because the
developers take shortcuts, thinking they can fix them later.

See, if product management is given this tool, they will take that
opportunity to cut the time-to market.

``Gee, we can fix it after the product is sold. Let's chop months
off the development time and blow it out the door, then put out a
patch.''

Engineering cannot come back with ``we must get this 100% right because
there is no way to fix it in the field, short of a very expensive
recall campaign.''
 
N

Nick Keighley

Ian Collins a écrit :







You can't use max as a function name. So what? You write a
"MyMax" function for a specific comparison if the need
would arise.



Of course you NEVER use two different types in your code.
If you would, it woudn't compile...

GREAT!

:)



Being a macro, the implementation will be hidden you think?
Or what?



Two philosophies here.

My family has an old fashioned car, (1998) that has NO SOFTWARE
in it. NOTHING.

My neighbours go to the garage very often, to get software updates
for their cars. They can only repair their cars at approved garages
that charge twice as much as independent ones. The software in their cars
can only be understood by special machines that are sold to a
selected few...

Yes, the trend to "better" cars is obvious. Auto makers are at last
in the position of eliminating all independent garages around.

Progress.

Went to the chinese shop and bought an alarm clock. It has no buttons
but a touch screen, full of incomprehensible symbols. To get into the
"settings" menu you have to touch a certain part of the screen, and put
the clock in  a special spacial orientation since the menus
change when you turn sideways the clock. It has a thick manual
that I had to read each time I set the alarm since it was so
complicated I always forgot.

BUT:

It told you also the temperature of
the room and had a wheather channel for 5 days. besides it
could tell ou the day of the week, the month, etc. You could
change the display to analogic or digital, maybe it would make the
coffee I did not read all the thick manual.

One day I got so fed up I threw it to the garden over the window
and bought another. It had 2 buttons, one for setting the time and
another for setting the alarm. No menus, no sofwtare no nothing.

PHEW!

I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure out
how to use my telephone.

- Bjarne Stroustroup
 
J

jacob navia

Nick Keighley a écrit :
I have always wished for my computer to be as easy to use as my
telephone; my wish has come true because I can no longer figure out
how to use my telephone.

- Bjarne Stroustroup

Exactly. Complexity is not a good feature.

That is why I insist that C has a future: it has less features. The whole
science of engineering is getting the balance right between NECESSARY
features and easy of use.

With a certain complexity level, computer languages become unusable.

jacob
 

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
474,438
Messages
2,571,699
Members
48,796
Latest member
Greg L.
Top