[LLVMbugs] [Bug 20825] "extern template" seems to invoke explicit instanciation but it should prohibit it.

bugzilla-daemon at llvm.org bugzilla-daemon at llvm.org
Tue Sep 2 13:17:35 PDT 2014


http://llvm.org/bugs/show_bug.cgi?id=20825

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> ---
I think you can forward declare your specialization of the static data member
like so:

// Start .h
template <typename T> class A {
public:
  static const char *info;
};

template <> const char *A<int>::info;
template <> const char *A<char>::info;

extern template class A<int>;
extern template class A<char>;

// Start .cpp
#include <iostream>

template <> const char *A<int>::info = "This is int";
template <> const char *A<char>::info = "This is char";

int main(int, char **) {
  std::cout << "A<int>:" << A<int>::info << "\n";
  std::cout << "A<char>:" << A<char>::info << "\n";
  return 0;
}

template class A<int>;
template class A<char>;

----

Otherwise, the compiler can do things like inline the generic, unspecialized
version of an inline function into your program.  Consider:

template <typename T>
struct A { static int f() { return sizeof(int); } };
extern template struct A<int>;
int main() {
  return A<int>::f();
}

After optimizations, main will return 4, even if some other TU contains:

template <> int A<int>::f() { return 5; };

This specialization can be similarly forward declared as:

template <> int A<int>::f();

-- 
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/20140902/a69aee96/attachment.html>


More information about the llvm-bugs mailing list