Inline question

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2007
Posts: 39
Reputation: Evan M is an unknown quantity at this point 
Solved Threads: 5
Evan M's Avatar
Evan M Evan M is offline Offline
Light Poster

Inline question

 
0
  #1
Oct 22nd, 2008
As I was busy inlining some of my functions, I was wondering if it is really necessary to inline anything. Doesn't the compiler automatically inline stuff, or do I have false memories? And if the compiler does inline stuff for me, would it be best to let it do its job or should I mess up something by doing it myself?

The reason I thought of the first question is because I can't get my now-inlined functions to work. A quick example:

test.cpp
  1. #include <iostream>
  2. #include <string>
  3. #include "test.h"
  4.  
  5. void main()
  6. {
  7. std::cout << getString();
  8. std::cin.get();
  9. }

test2.cpp
  1. #include "test.h"
  2.  
  3. inline
  4. std::string getString()
  5. {
  6. return "Beam me up, Scotty!";
  7. }

test.h
  1. #ifndef TEST_H
  2. #define TEST_H
  3.  
  4. #include <string>
  5.  
  6. std::string getString();
  7.  
  8. #endif

This generates the error:
  1. test.obj : error LNK2019: unresolved external symbol "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl getString(void)" (?getString@@YA?AV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function _main
  2. C:\w\e\Projects\test\Debug\test.exe : fatal error LNK1120: 1 unresolved externals

Removing the inline will make the program work.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,484
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1478
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Inline question

 
0
  #2
Oct 22nd, 2008
Compilers are free to choose to ignore the inline keyword or not.

Move the code in test2.cpp to test2.h since you use the linline keyword, then delete test2.cpp from the project.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 40
Reputation: seanhunt is an unknown quantity at this point 
Solved Threads: 6
seanhunt seanhunt is offline Offline
Light Poster

Re: Inline question

 
1
  #3
Oct 22nd, 2008
The function definition is not being included in your test.cpp module, so the linker can't find it. Often inline function definitions are saved as a different type of file (like test.inl) which is itself #include -ed.

if you really need to inline something and aren't sure whether the compiler will or not, standard practice is to write the function definition in the function declaration. In other words move the code into test.h as follows:

  1. #ifndef TEST_H
  2. #define TEST_H
  3. #include <string>
  4.  
  5. std::string getString(){ return "Beam me up, Scotty!"; };
  6.  
  7. #endif

or include the code module as well...usually instead of being saved as a .cpp the file is saved as another type (often .inl) which you will also have to #include.

As to whether it's worth doing, that depends. If it is absolutely time-critical code and you are unsure as to whether the compiler will or will not inline functions, and it is worth the code bloat, sure. Otherwise, letting the compiler decide when to inline code...and with compilers you can change how aggressive they are about inlining code, also. I can't really see the need for it in a function like the one you mentioned...it's more often seen when writing very time critical math functions and such.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Inline question

 
0
  #4
Oct 22nd, 2008
Don't forget, main returns int
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 39
Reputation: Evan M is an unknown quantity at this point 
Solved Threads: 5
Evan M's Avatar
Evan M Evan M is offline Offline
Light Poster

Re: Inline question

 
0
  #5
Oct 22nd, 2008
Thanks for the help.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC