[LC++]Inlining with templated member functions

Bill Rausch William.Rausch at numerical.com
Thu Jul 4 00:00:25 UTC 2002


If it is inlined, then every time it is called the compiler needs to 
see the definition in order to expand the call.  If your header had 
included the "inline" keyword but not the function body then it would 
have complained when it was compiling. Since there was no inline 
keyword in the header, the compiler just did a normal external call 
to a function. Then the linker said "but that function doesn't exist 
anywhere".

When you use inline in the header, then of course the compiler 
expects to find the rest of the definition there as well, just like 
old style C macros.

Bill

At 5:48 PM +0930 7/1/02, Mark Phillips wrote:
>Hi,
>
>This list has been a bit quiet.  I have a question though...
>
>I defined a struct with a templated member function thus:
>
>struct MyStruct {
>...
>   template<typename A> void put(A const& a);
>...
>};
>
>and later did
>
>template<typename A>
>inline void put(A const& a) {
>... // do something
>}
>
>and in one of my .cc files, I called the "put" function.  I am using
>g++ version 2.96, and the linker complained with something along the
>lines of:
>
>    undefined reference to void put<Something>(Something const&)
>
>But there should be no need to have a reference, because it should
>be inlined!
>
>I moved the implementation of put to inside the struct definition
>like so:
>
>struct MyStruct {
>...
>   template<typename A> void put(A const& a) {
>   ... // do something
>   }
>...
>};
>
>and then magically it worked!
>
>Does anyone know anything about why it didn't like the inlining being
>done outside the struct definition?
>
>Thanks,
>
>Mark.
>
>--
>Dr Mark H Phillips
>Research Analyst (Mathematician)
>
>AUSTRICS - smarter scheduling solutions - www.austrics.com
>
>Level 2, 50 Pirie Street, Adelaide SA 5000, Australia
>Phone +61 8 8226 9850
>Fax   +61 8 8231 4821
>Email mark at austrics.com.au
>
>
>_______________________________________________
>This is the Linux C++ Programming List
>: http://lists.linux.org.au/listinfo/tuxcpprogramming List


-- 
Bill Rausch, Software Development, UNIX, Mac, Windows
Numerical Applications, Richland, WA  509-943-0861




More information about the tuxCPProgramming mailing list