[llvm-bugs] [Bug 45351] function static variable instantiated at -O2 despite extern template

via llvm-bugs llvm-bugs at lists.llvm.org
Mon Mar 30 11:36:12 PDT 2020


https://bugs.llvm.org/show_bug.cgi?id=45351

Reid Kleckner <rnk at google.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |rnk at google.com
         Resolution|---                         |INVALID

--- Comment #1 from Reid Kleckner <rnk at google.com> ---
Clang is allowed to do this inlining. I can't site the standard, but this came
up for me personally while adding extern template decls to LLVM.

Function templates that are declared extern can be inlined if they are marked
inline. Defining a method inside a class body implicitly makes it inline. So,
to avoid the inlining without resorting to fno-inline, write the code like
this:

template <typename T>
struct S {
  int func();
};

template <typename T>
int S<T>::func() {
  static int function_local_static = 0;
  return ++function_local_static;
}

extern template class S<int>;

int user()
{
    return S<int>().func();
}

This will prohibit instantiation of S::func and even allow you to specialize it
out of line.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-bugs/attachments/20200330/456d2a50/attachment-0001.html>


More information about the llvm-bugs mailing list