This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 113d. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2024-04-05


412. Can a replacement allocation function be inline?

Section: 9.2.3  [dcl.fct.spec]     Status: NAD     Submitter: Matt Austern     Date: 23 Apr 2003

A customer reports that when he attempts to replace ::operator new with a user-defined function, the standard library calls the default function by preference if the user-defined function is inline. I believe that our compiler is correct, and that such a replacement function isn't allowed to be inline, but I'm not sure there's sufficiently explicit language in the standard.

In general, of course, the definition of an inline function must be present in every translation unit where the function is called. (9.2.3 [dcl.fct.spec], par 4) It could be argued that this requirement doesn't quite address replacement functions: what we're dealing with is the odd case where we've already got one definition and the user is supplying a different one. I'd like to see something specifically addressing the case of a replacement function.

So what do we have? I see discussion of requirement for a replacement ::operator new in three places: 16.4.5.6 [replacement.functions], 17.6.3.2 [new.delete.single] par 2, and 6.7.5.5 [basic.stc.dynamic] par 2-3. I don't see anything explicitly saying that the replacement function may not be inline. The closest I can find is 17.6.3.2 [new.delete.single] par 2, which says that "a C++ program may define a function with this function signature that displaces the default version defined by the C++ Standard library". One might argue that "with this function signature" rules out inline, but that strikes me as a slight stretch.

Have I missed anything?

Andrew Koenig: I think you've turned up a problem in 9.2.3 [dcl.fct.spec] paragraph 4. Consider:

  // Translation unit 1
  #include <iostream>

  extern void foo(void (*)());

  inline void bar() {
    std::cout << "Hello, world!" << std::endl;
  }

  int main() {
    foo(bar);
  }

  // Translation unit 2
  void foo(void (*f)()) { (*f)(); }

Are you really trying to tell me that this program is ill-formed because the definition of bar is not available in translation unit 2?

I think not. The actual words in 9.2.3 [dcl.fct.spec] par 4 are

An inline function shall be defined in every translation unit in which it is used...
and I think at in this context, ``used'' should be interpreted to mean that foo is used only in translation unit 1, where it is converted to a value of type void(*)().

Notes from October 2003 meeting:

We don't think Andy Koenig's comment requires any action; "used" is already defined appropriately.

We agree that this replacement should not be allowed, but we think it's a library issue (in the rules for allowed replacements). Forwarded to library group; it's issue 404 on the library issues list.