public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/63267] New: Static member of template class is not constructed before it's used.
@ 2014-09-15  8:07 yuxiao1985 at hotmail dot com
  2014-09-15  9:01 ` [Bug c++/63267] " redi at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: yuxiao1985 at hotmail dot com @ 2014-09-15  8:07 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 63267
           Summary: Static member of template class is not constructed
                    before it's used.
           Product: gcc
           Version: 4.4.7
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: yuxiao1985 at hotmail dot com

// test.cpp
#include <iostream>
using namespace std;

struct Output
{
    Output();
    void doIt() { cout<<"do it a:"<<a<<"\n";}
    int a;
};

Output::Output(): a(100)
{
    cout<<"Output constructed\n";
}

template <class T>
class Test
{
    public:
   static void use(const Output& ) { }
   static int callStaticMember();
   static Output out;
};

template <class T>
Output Test<T>::out;

template <class T>
int Test<T>::callStaticMember()
{
    use(out);
    out.doIt();
    return 0;
}

int a = Test<int>::callStaticMember();

int main()
{
    return 0;
}


The result is not correct. the static member should be constructed first.

----------------------------
[yxiao2@centos:~]$ g++ test.cpp
[yxiao2@centos:~]$ ./a.out
do it a:0
Output constructed


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/63267] Static member of template class is not constructed before it's used.
  2014-09-15  8:07 [Bug c++/63267] New: Static member of template class is not constructed before it's used yuxiao1985 at hotmail dot com
@ 2014-09-15  9:01 ` redi at gcc dot gnu.org
  2014-09-17 13:57 ` paolo.carlini at oracle dot com
  2021-08-09 19:51 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: redi at gcc dot gnu.org @ 2014-09-15  9:01 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
I think GCC's behaviour is allowed by the standard. You do not explicitly
instantiate the static member 'Test<int>::out', so the compiler provides an
implicit instantiation which is placed at the end of the file, so it is
initialized after the variable 'a'.

See http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#993 which
clarified this result is permitted.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/63267] Static member of template class is not constructed before it's used.
  2014-09-15  8:07 [Bug c++/63267] New: Static member of template class is not constructed before it's used yuxiao1985 at hotmail dot com
  2014-09-15  9:01 ` [Bug c++/63267] " redi at gcc dot gnu.org
@ 2014-09-17 13:57 ` paolo.carlini at oracle dot com
  2021-08-09 19:51 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: paolo.carlini at oracle dot com @ 2014-09-17 13:57 UTC (permalink / raw)
  To: gcc-bugs

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

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|major                       |normal


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/63267] Static member of template class is not constructed before it's used.
  2014-09-15  8:07 [Bug c++/63267] New: Static member of template class is not constructed before it's used yuxiao1985 at hotmail dot com
  2014-09-15  9:01 ` [Bug c++/63267] " redi at gcc dot gnu.org
  2014-09-17 13:57 ` paolo.carlini at oracle dot com
@ 2021-08-09 19:51 ` pinskia at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-08-09 19:51 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
ICC and GCC produce the same result while clang produces:

Output constructed
do it a: 100


Note I noticed if I used std::cout, I get a crash with clang even.

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-08-09 19:51 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2014-09-15  8:07 [Bug c++/63267] New: Static member of template class is not constructed before it's used yuxiao1985 at hotmail dot com
2014-09-15  9:01 ` [Bug c++/63267] " redi at gcc dot gnu.org
2014-09-17 13:57 ` paolo.carlini at oracle dot com
2021-08-09 19:51 ` pinskia 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).