Standard behaviour ?

Discussion in 'C++' started by Ares Lagae, Nov 26, 2003.

  1. Ares Lagae

    Ares Lagae Guest

    Could someone please tell me what the behaviour of the following code
    snippet is, according to the C++ standard ?

    --------------------------------------------------------------------------

    #include <iostream>

    template <class T>
    class Foo
    {
    public:
    T m_f;
    Foo(T f) : m_f(f) {}
    //Foo(const Foo & foo) : m_f(foo.m_f) {}
    static const Foo CONST;
    };

    template <class T>
    const Foo<T> Foo<T>::CONST = Foo(T(1));

    class Bar
    {
    public:
    Foo<float> m_foo;
    Bar() : m_foo(Foo<float>::CONST)
    {
    std::cout << "Foo.m_f = " << m_foo.m_f << std::endl;
    }
    };

    Bar bar;

    int main(int argc, char * argv[])
    {
    Bar bar;
    return 0;
    }

    --------------------------------------------------------------------------------

    On some compilers it prints "0 1", as if the static constant was not
    initialized the first time, and on some compilers it prints "1 1".
    I *think* (i hope not, but i could be wrong here) than the standard
    behaviour is to print "1 1".
    If this is in fact the case, there is a bug in each version of the MS C
    compiler (tested with MSVC 6.0 and MSVC 7.0), the Intel compiler (tested
    with ICC7.0/win), and in some versions of GCC (with gcc 3.3 it does
    seems to work). If this is in fact a bug, is this bug known ?

    Best regards,
    Ares Lagae
    Ares Lagae, Nov 26, 2003
    #1
    1. Advertising

  2. Ares Lagae

    Ares Lagae Guest

    Ares Lagae wrote:
    >
    > Could someone please tell me what the behaviour of the following code
    > snippet is, according to the C++ standard ?
    >


    guess i was to fast to post, sorry

    because both Bar bar and Foo::CONST are statics, there is no order
    defined in which they are initialized ... so the behaviour according to
    the standard is undefined. It just took me a while to realize that :)

    best regards,
    Ares Lagae
    Ares Lagae, Nov 26, 2003
    #2
    1. Advertising

  3. "Ares Lagae" <> skrev i en meddelelse
    news:...
    > Ares Lagae wrote:
    > >
    > > Could someone please tell me what the behaviour of the following code
    > > snippet is, according to the C++ standard ?
    > >

    >
    > guess i was to fast to post, sorry
    >
    > because both Bar bar and Foo::CONST are statics, there is no order
    > defined in which they are initialized ... so the behaviour according to
    > the standard is undefined. It just took me a while to realize that :)
    >
    > best regards,
    > Ares Lagae


    This is not entirely correct - within a translation unit (one source file),
    the order is well defined. However, I do have some severe reservation wrt
    your definition of templated static members - i have never used those myself
    and wonder if they have any consequences for the initialization.

    Kind regards
    Peter Koch Larsen
    Peter Koch Larsen, Nov 26, 2003
    #3
  4. "Ares Lagae" <> wrote...
    > Ares Lagae wrote:
    > >
    > > Could someone please tell me what the behaviour of the following code
    > > snippet is, according to the C++ standard ?
    > >

    >
    > guess i was to fast to post, sorry
    >
    > because both Bar bar and Foo::CONST are statics, there is no order
    > defined in which they are initialized ... so the behaviour according to
    > the standard is undefined. It just took me a while to realize that :)


    Actually, I don't think so. The global 'bar' causes the constructor to
    be called. In the constructor the initialisation list causes the static
    member of the Foo template to be implicitly defined (per 14.7.1/1) which
    causes the initialisation of it to float(1). The 'm_foo' member is then
    initialised with the value of the 'CONST', which has been itself
    initialised to 1f. Therefore, the output has to be

    Foo.m_f = 1
    Foo.m_f = 1

    If you have any proof of why the behaviour is undefined, do tell.

    Victor
    Victor Bazarov, Nov 26, 2003
    #4
  5. Ares Lagae

    Ares Lagae Guest

    > Actually, I don't think so. The global 'bar' causes the constructor to
    > be called. In the constructor the initialisation list causes the static
    > member of the Foo template to be implicitly defined (per 14.7.1/1) which
    > causes the initialisation of it to float(1). The 'm_foo' member is then
    > initialised with the value of the 'CONST', which has been itself
    > initialised to 1f. Therefore, the output has to be
    >
    > Foo.m_f = 1
    > Foo.m_f = 1
    >
    > If you have any proof of why the behaviour is undefined, do tell.


    If this is indeed the case, then most compilers do not implement this
    behaviour. Of the compilers i tested (see my previous post), only the most
    recent g++ (3.3) behaved as you describe.

    best regards,
    Ares Lagae
    Ares Lagae, Nov 26, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bruce varley
    Replies:
    2
    Views:
    392
    Mike Wahler
    Nov 25, 2003
  2. eddiew_AUS
    Replies:
    18
    Views:
    642
    Andrey Tarasevich
    Jan 27, 2004
  3. Replies:
    2
    Views:
    438
    Larry I Smith
    May 29, 2005
  4. Batista, Facundo

    Standard behaviour of a getSomething method

    Batista, Facundo, Jul 23, 2003, in forum: Python
    Replies:
    2
    Views:
    288
    Dan Williams
    Jul 24, 2003
  5. Andy Chambers
    Replies:
    1
    Views:
    383
    Daniel Dyer
    May 14, 2007
Loading...

Share This Page