Standard behaviour ?

From: Ares Lagae (ares_at_cs.kuleuven.ac.be)
Date: 11/26/03


Date: Wed, 26 Nov 2003 14:46:38 +0100

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



Relevant Pages

  • Re: Buffer overflows and asctime()
    ... to contain a "bug" as others are not. ... standard the year member receives a maximum value. ... will overflow its buffer if confronted with valid inputs. ... So you still do not understand "undefined behavior". ...
    (comp.std.c)
  • Re: Converting CString to Integer
    ... >a lower price and with life time guarantee is better, ... it contained a bad bug. ... Elsewhere, you've tried quoting the C++ Standard, so I ... I showed how to use strtol correctly in an earlier message. ...
    (microsoft.public.vc.mfc)
  • Re: GCD(0,0)
    ... >> standard, nearly universal, while for others there are competing ... >> well as many that bar it. ... Many abstract algebra and ring ... >| If you unfold the definition if GCD you will see, that GCD ...
    (sci.math)
  • Re: STL-Streams und Unicode/UTF-16
    ... Template von Haus aus eine Eierlegende Wollmilchsau ist. ... eigene Facette schreibt und diese dem Stream gibt. ... codecvt-Facette vorhanden ist (der Standard verlangt das). ...
    (microsoft.public.de.vc)
  • Re: Optimisation and INTENT
    ... If BAR calls another function and passes B as an argument, ... No, not in standard Fortran. ... Fortran program. ... an interesting and useful optimization. ...
    (comp.lang.fortran)