which is used by two files, foo.cpp and bar.cpp. Both contain functions of class foo but there were too many so I split them up.
When I tried to compile the program, I got an "undefined function as_bytes()" when it linked in bar.cpp. After much playing around because I thought the bug was elsewhere, I found out that passing -fno-inline to g++ (4.4.5) solved my problem. By default, I was using the -O3 optimization flag, but even so, why would it silently inline something that I didn't designate as inline? And is there something wrong with what I coded (I realize I've given very little source code above...)?
I understand if a member function is included in the class definition with a body, it is inlined. I haven't done that (AFAIK). Is there another rule for inlining member functions?
There is the keyword "inline"; I was wondering if there is a keyword that is equivalent to "don't ever inline!" that I could insert into my code at where as_bytes () is defined.
Actually the inline keyword is just a hint to the compiler to do in-lining for you. It does not mean you put inline, the compiler must mandatory make it that way.
Which is why nowadays modern C++ compiler, even if you don't put inline but you put the program code inside the .h files, it will automatically in-line for you. Likewise if compiler found your code too long and too complex to in-line, even you have in-line keyword it will not in-line for you.
You have this declaration in a header file presumably? You should move the definition into the header file also. For most compilers, the complete template class definition must be visible for the compiler to generate the code. For just template functions, it is probably still easiest to move it to the header file. You can still define it outside of the class such that it does not default to being inlined. It my still be inlined if the compiler thinks it would be worth it though. I often have a class.h with the "public" interface and a class_implementation.h with the internal details that still need to be in a header.
Thank you for your replies and the explanations about inlining.
jimc -- what you described is exactly the problem I was having. Indeed, I was treating the template "as a function" and had it defined in one .cpp file when I should have put it in the header file -- I didn't know that. Your explanation and the longer one in the FAQ helped greatly; thank you!