public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "tobias.bruell at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/96853] New: Explicit template instantiation & thread_local interaction
Date: Sun, 30 Aug 2020 18:45:14 +0000	[thread overview]
Message-ID: <bug-96853-4@http.gcc.gnu.org/bugzilla/> (raw)

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96853

            Bug ID: 96853
           Summary: Explicit template instantiation & thread_local
                    interaction
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: tobias.bruell at gmail dot com
  Target Milestone: ---

Compiling the below with

> g++-10 main.cpp library.cpp -o main

in gcc 10.1 leads to an executable "main" that segfaults. The problem seems to
be that the out-commented "extern" declarations in library.hpp are missing.

So, the program might be UB, but it would be nice to get an error message from
the compiler (as Clang does).

Otherwise, the linker could give an error: the final executable "main" contains
a "callq 0x0" instruction (on x86_64) if you also add "-static" to the above
command-line. I think the problem is that "_ZTHN7library3FooIiE1fE" (TLS init
function for library::Foo<int>::f) is missing from any of the intermediate
object files.

--------------------------------
--------- library.hpp ----------

#ifndef INCLUDE_GUARD_LIBRARY_H_
#define INCLUDE_GUARD_LIBRARY_H_

namespace library
{
  template<typename T>
  struct Foo
  {
    using Func = T (*) (T);
    static thread_local Func f;
  };

  //extern template struct Foo<char>;
  //extern template struct Foo<int>;
  //extern template struct Foo<long>;
}

#endif

-------------------------------
--------- library.cpp ---------

#include "library.hpp"

namespace library
{
  template<typename T>
  T core_function (T val)
  {
    return val * 2;
  }

  template<typename T>
  thread_local
  typename Foo<T>::Func
  Foo<T>::f = &core_function<T>;

  template struct Foo<char>;
  template struct Foo<int>;
  template struct Foo<long>;
}

--------------------------------
----------- main.cpp -----------

#include "library.hpp"

int main () {
  return library::Foo<int>::f(2);
}

             reply	other threads:[~2020-08-30 18:45 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-30 18:45 tobias.bruell at gmail dot com [this message]
2024-06-23 20:06 ` [Bug c++/96853] " nsoft.sagaonline at gmail dot com

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-96853-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).