home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!cis.ohio-state.edu!magnus.acs.ohio-state.edu!usenet.ins.cwru.edu!agate!forney.berkeley.edu!jbuck
- From: jbuck@forney.berkeley.edu (Joe Buck)
- Newsgroups: comp.lang.c++
- Subject: Re: risky inline
- Date: 27 Aug 1992 18:54:42 GMT
- Organization: U. C. Berkeley
- Lines: 39
- Message-ID: <17j8diINNo0p@agate.berkeley.edu>
- References: <1992Aug27.142700@uni-paderborn.de>
- NNTP-Posting-Host: forney.berkeley.edu
- Keywords: inline, safe programming
-
- In article <1992Aug27.142700@uni-paderborn.de> trachos@uni-paderborn.de (Konstantin Trachos) writes:
- >Inline-expansion is surely more effective than #defines, but it isn't
- >less risky than that.
-
- Inline expansion is FAR less risky than macros, because type-checking is
- done and because the parsing is the same as if it were a regular function.
- However, inline functions have file scope only (I would say they are
- static, but unfortunately Stroustrup gave that word one too many
- meanings). For this reason you must never do what your example does,
- which I abbreviate:
-
- >// Interface C.hxx -----------------------------
- > class C{
- > int c();
- > };
- >
- >// Code C.cxx ----------------------------------
- > #include "C.hxx"
- > inline int C::c() {return 1;}// !
-
- You've basically put a lie in your C.hxx file: you've told users of
- that file that someone will supply a global C::c(), but C.cxx supplies
- only a local C::c().
-
- The solution is that if you put a declaration of a function in a header
- file, you must supply a global definition somewhere (it can't be inline).
- If you want an inline function, it must be in the header file. With some
- compilers there are ways around this, but if your code is to run under
- several compilers, you need to follow this rule.
-
- But to claim on the basis of this one little gotcha that inline functions
- are not any less risky than macros is to lose all sense of proportion.
- It's evident that you didn't understand the rule about the local scope
- about inline functions.
-
-
-
- --
- Joe Buck jbuck@ohm.berkeley.edu
-