public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/109464] New: gcc does not instantiate constructor for explicitly instantiated template
@ 2023-04-10 14:15 lh_mouse at 126 dot com
2023-04-10 20:50 ` [Bug c++/109464] " pinskia at gcc dot gnu.org
` (7 more replies)
0 siblings, 8 replies; 9+ messages in thread
From: lh_mouse at 126 dot com @ 2023-04-10 14:15 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109464
Bug ID: 109464
Summary: gcc does not instantiate constructor for explicitly
instantiated template
Product: gcc
Version: 12.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: lh_mouse at 126 dot com
Target Milestone: ---
Created attachment 54824
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=54824&action=edit
unreduced testcase
Attached is a preprocessed file. I don't know how to reduce it for absence of a
function.
```
template<typename charT>
class basic_shallow_string
{
static_assert(!is_array<charT>::value, "invalid character type");
static_assert(is_trivial<charT>::value, "characters must be trivial");
template<typename, typename> friend class basic_cow_string;
private:
const charT* m_ptr;
size_t m_len;
public:
explicit constexpr
basic_shallow_string(const charT* ptr) noexcept
: m_ptr(ptr), m_len(noadl::xstrlen(ptr))
{ }
public:
constexpr
const charT*
c_str() const noexcept
{ return this->m_ptr; }
constexpr
size_t
length() const noexcept
{ return this->m_len; }
(... more member functions follow ...)
};
extern template class basic_shallow_string<char>;
```
And in 'cow_string.cpp' I have
```
#include "cow_string.hpp"
namespace rocket {
template class basic_shallow_string<char>;
(... more explicit instantiations follow ...)
} // namespace rocket
```
This explicit instantiation instantiates all inline member functions above, but
not the constructor. I have examined assembly output, and there is no
constructor. Unless optimization is enabled, this causes undefined references:
```
/usr/bin/ld: asteria/repl/bin_asteria-commands.o: in function
`rocket::basic_shallow_string<char> rocket::sref<char>(char const*)':
/home/lh_mouse/GitHub/asteria/asteria/repl/../../rocket/cow_string.hpp:97:
undefined reference to
`rocket::basic_shallow_string<char>::basic_shallow_string(char const*)'
collect2: error: ld returned 1 exit status
```
This happens only with the constructor.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/109464] gcc does not instantiate constructor for explicitly instantiated template
2023-04-10 14:15 [Bug c++/109464] New: gcc does not instantiate constructor for explicitly instantiated template lh_mouse at 126 dot com
@ 2023-04-10 20:50 ` pinskia at gcc dot gnu.org
2023-04-11 3:50 ` lh_mouse at 126 dot com
` (6 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-10 20:50 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109464
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Created attachment 54825
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=54825&action=edit
failed attempt at reducing
This was my trial on reducing the testcase but it failed. In that the symbol is
defined correctly still.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/109464] gcc does not instantiate constructor for explicitly instantiated template
2023-04-10 14:15 [Bug c++/109464] New: gcc does not instantiate constructor for explicitly instantiated template lh_mouse at 126 dot com
2023-04-10 20:50 ` [Bug c++/109464] " pinskia at gcc dot gnu.org
@ 2023-04-11 3:50 ` lh_mouse at 126 dot com
2023-04-11 3:55 ` pinskia at gcc dot gnu.org
` (5 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: lh_mouse at 126 dot com @ 2023-04-11 3:50 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109464
--- Comment #2 from LIU Hao <lh_mouse at 126 dot com> ---
shouldn't this be classified as wrong code?
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/109464] gcc does not instantiate constructor for explicitly instantiated template
2023-04-10 14:15 [Bug c++/109464] New: gcc does not instantiate constructor for explicitly instantiated template lh_mouse at 126 dot com
2023-04-10 20:50 ` [Bug c++/109464] " pinskia at gcc dot gnu.org
2023-04-11 3:50 ` lh_mouse at 126 dot com
@ 2023-04-11 3:55 ` pinskia at gcc dot gnu.org
2023-04-11 4:44 ` lh_mouse at 126 dot com
` (4 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-11 3:55 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109464
--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to LIU Hao from comment #2)
> shouldn't this be classified as wrong code?
It is at least a link-failure. Wrong code might be a strech here.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/109464] gcc does not instantiate constructor for explicitly instantiated template
2023-04-10 14:15 [Bug c++/109464] New: gcc does not instantiate constructor for explicitly instantiated template lh_mouse at 126 dot com
` (2 preceding siblings ...)
2023-04-11 3:55 ` pinskia at gcc dot gnu.org
@ 2023-04-11 4:44 ` lh_mouse at 126 dot com
2023-04-11 6:16 ` lh_mouse at 126 dot com
` (3 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: lh_mouse at 126 dot com @ 2023-04-11 4:44 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109464
LIU Hao <lh_mouse at 126 dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
Known to fail| |10.4.0, 9.5.0
--- Comment #4 from LIU Hao <lh_mouse at 126 dot com> ---
Manually reduced testcase:
(godbolt: https://gcc.godbolt.org/z/YsaoM78vM)
```
template<typename T>
struct shallow
{
int len;
constexpr shallow() : len(0) { }
};
template<typename T>
struct use_shallow
{
static constexpr shallow<T> s_zstr = { };
static_assert(s_zstr.len == 0);
};
extern template struct shallow<char>;
extern template struct use_shallow<char>;
template struct shallow<char>;
template struct use_shallow<char>;
```
Constructor of `shallow` is lost. All GCC versions are affected.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/109464] gcc does not instantiate constructor for explicitly instantiated template
2023-04-10 14:15 [Bug c++/109464] New: gcc does not instantiate constructor for explicitly instantiated template lh_mouse at 126 dot com
` (3 preceding siblings ...)
2023-04-11 4:44 ` lh_mouse at 126 dot com
@ 2023-04-11 6:16 ` lh_mouse at 126 dot com
2024-07-15 13:48 ` ppalka at gcc dot gnu.org
` (2 subsequent siblings)
7 siblings, 0 replies; 9+ messages in thread
From: lh_mouse at 126 dot com @ 2023-04-11 6:16 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109464
--- Comment #5 from LIU Hao <lh_mouse at 126 dot com> ---
Additional information:
I tried splitting the two class templates into two separate .cpp files, so the
explicit instantiation of `basic_shallow_string<char>` should not be subject to
the instantiation of `basic_cow_string<char>`. This made GCC emit the
constructor correctly (checked by examining assembly), but still failed to the
very same undefined reference.
At the moment, a temporary workaround is to mark the constructor of
`basic_shallow_string` as `always_inline`. This works for GCC 10, 11 and 12.
Haven't checked GCC 9 as it's no longer available from APT sources of my
current machine.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/109464] gcc does not instantiate constructor for explicitly instantiated template
2023-04-10 14:15 [Bug c++/109464] New: gcc does not instantiate constructor for explicitly instantiated template lh_mouse at 126 dot com
` (4 preceding siblings ...)
2023-04-11 6:16 ` lh_mouse at 126 dot com
@ 2024-07-15 13:48 ` ppalka at gcc dot gnu.org
2024-07-15 14:13 ` lh_mouse at 126 dot com
2024-07-19 15:08 ` cvs-commit at gcc dot gnu.org
7 siblings, 0 replies; 9+ messages in thread
From: ppalka at gcc dot gnu.org @ 2024-07-15 13:48 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109464
Patrick Palka <ppalka at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
See Also| |https://gcc.gnu.org/bugzill
| |a/show_bug.cgi?id=113208
CC| |ppalka at gcc dot gnu.org
--- Comment #6 from Patrick Palka <ppalka at gcc dot gnu.org> ---
It seems this is fixed on trunk since r15-521-g6ad7ca1bb90573?
I see that trunk now emits the shallow() ctor:
shallow<char>::shallow() [base object constructor]:
mov DWORD PTR [rdi], 0
ret
use_shallow<char>::s_zstr:
.zero 4
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/109464] gcc does not instantiate constructor for explicitly instantiated template
2023-04-10 14:15 [Bug c++/109464] New: gcc does not instantiate constructor for explicitly instantiated template lh_mouse at 126 dot com
` (5 preceding siblings ...)
2024-07-15 13:48 ` ppalka at gcc dot gnu.org
@ 2024-07-15 14:13 ` lh_mouse at 126 dot com
2024-07-19 15:08 ` cvs-commit at gcc dot gnu.org
7 siblings, 0 replies; 9+ messages in thread
From: lh_mouse at 126 dot com @ 2024-07-15 14:13 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109464
LIU Hao <lh_mouse at 126 dot com> changed:
What |Removed |Added
----------------------------------------------------------------------------
Resolution|--- |FIXED
Status|UNCONFIRMED |RESOLVED
--- Comment #7 from LIU Hao <lh_mouse at 126 dot com> ---
Yes, looks so.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [Bug c++/109464] gcc does not instantiate constructor for explicitly instantiated template
2023-04-10 14:15 [Bug c++/109464] New: gcc does not instantiate constructor for explicitly instantiated template lh_mouse at 126 dot com
` (6 preceding siblings ...)
2024-07-15 14:13 ` lh_mouse at 126 dot com
@ 2024-07-19 15:08 ` cvs-commit at gcc dot gnu.org
7 siblings, 0 replies; 9+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2024-07-19 15:08 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109464
--- Comment #8 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Patrick Palka <ppalka@gcc.gnu.org>:
https://gcc.gnu.org/g:58a9f3ded1a0ccc2c8b0a42f976950041734798e
commit r15-2154-g58a9f3ded1a0ccc2c8b0a42f976950041734798e
Author: Patrick Palka <ppalka@redhat.com>
Date: Fri Jul 19 11:08:09 2024 -0400
c++: add fixed testcase [PR109464]
Seems to be fixed by r15-521-g6ad7ca1bb90573.
PR c++/109464
gcc/testsuite/ChangeLog:
* g++.dg/template/explicit-instantiation8.C: New test.
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2024-07-19 15:08 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-10 14:15 [Bug c++/109464] New: gcc does not instantiate constructor for explicitly instantiated template lh_mouse at 126 dot com
2023-04-10 20:50 ` [Bug c++/109464] " pinskia at gcc dot gnu.org
2023-04-11 3:50 ` lh_mouse at 126 dot com
2023-04-11 3:55 ` pinskia at gcc dot gnu.org
2023-04-11 4:44 ` lh_mouse at 126 dot com
2023-04-11 6:16 ` lh_mouse at 126 dot com
2024-07-15 13:48 ` ppalka at gcc dot gnu.org
2024-07-15 14:13 ` lh_mouse at 126 dot com
2024-07-19 15:08 ` cvs-commit at gcc dot gnu.org
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).