std: want to use for_each !..

Started by
25 comments, last by Seriema 20 years, 10 months ago
quote:Original post by Seriema
petewood: I was under the impression that templates only inline under the same conditions as normal inlines, no loops etc? as always, inlines are only a recommendation to the compiler. It''ll probably inline it anyway (since it''s just two rows), but I put it there more for the coder than the compiler.


Unfortunetly, there are two concepts called ''inline''. The C++ keyword has little to do with what you are talking about. When you declare a function inline, you are telling the compiler that this function is being declared in a header, and not to produce duplicates of it when compiling so the linker does not get confused.

//header.h
void test()
{
//...
}


//source1.cpp
include header.h

//source2.cpp
include header.h

When it links, you''ll get an error about two functions both called test.

inline void test() fixes the problem.


__inline, __forceinline et. al. will give the compiler hints to inline the code (and are not part of the standard).
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Advertisement
when it''s a class member though inline doesn''t matter, does it?
quote:Original post by petewood
when it's a class member though inline doesn't matter, does it?


I guess you mean this:
class A{public  void f(){ /* do stuff */} // implicit inline};  


and that gives you inlining it has the same effect as:

class B{public:  inline void g();// explicit inline};inline void B::g(){ /* do other stuff */  


[edited by - DigitalDelusion on May 30, 2003 4:36:12 AM]
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
Hmm...
void operator () ( T*& ptr ) const 

Isn''t a pointer always passed by reference? Enlighten me, please
And the price we paid was the price men have always paid for achieving paradise in this life -- we went soft, we lost our edge. - "Muad'Dib: Conversations" by the Princess Irulan
This would help in the following way:

char* data = new char[1024];delete[] data;// .. lots of code so that we forgot our deletionfor(int i=0; i < 1024; i++) data[i] = 0; //uhoh, data pointed at deallocated memory  

The people writing the safe_delete function want it to delete the memory and change the pointer's value to zero . That way they don't have to worry about setting it to zero manually.

So if we want to modify the address stored in the pointer we must pass it by reference. Without it, setting the argument to zero would affect only the function's pointer, not the caller's.

4th edit: bad day...

[edited by - antareus on May 30, 2003 5:22:07 PM]
--God has paid us the intolerable compliment of loving us, in the deepest, most tragic, most inexorable sense.- C.S. Lewis
quote:Original post by Jedyte
Hmm...
void operator () ( T*& ptr ) const  

Isn''t a pointer always passed by reference? Enlighten me, please
Pointer is passed by value. Try this
void test1(int*& number) {  number = 0; //changes the pointer that was passed by reference}void test2(int* number) {  number = 0; //changes the local pointer}int main() {  int a = 1;  int b = 2;  int* pA = &a  int* pB = &b  if (pA) cout << *pA << endl;  if (pB) cout << *pB << endl;  test1(pA);  test2(pB);  if (pA) cout << *pA << endl;  if (pB) cout << *pB << endl;}

And you''ll see that it''ll print ''1'', ''2'' and ''2''. It won''t print *pA twice since it changed to 0 inside test1. pB didn''t change, only the local pointer inside test2 changed.
Ah I see, of course.
Thanks civguy.
And the price we paid was the price men have always paid for achieving paradise in this life -- we went soft, we lost our edge. - "Muad'Dib: Conversations" by the Princess Irulan

This topic is closed to new replies.

Advertisement