473,385 Members | 1,470 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Standard behaviour ?

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
Jul 22 '05 #1
4 1525
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
Jul 22 '05 #2

"Ares Lagae" <ar**@cs.kuleuven.ac.be> skrev i en meddelelse
news:3F***************@cs.kuleuven.ac.be...
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
Jul 22 '05 #3
"Ares Lagae" <ar**@cs.kuleuven.ac.be> 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
Jul 22 '05 #4
> 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
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Eric Boutin | last post by:
Hi ! currently reading C docs, I think I'm reading docs about the stdc lib, but I'm not shure.. it talks about signals, does *all* OS implement the signal function, and use it well ? I mean.. I...
2
by: Luke Wu | last post by:
Hi, I'm finding myself reinventing the wheel too much in my code (because I don't know all the functions available in the Standard library) and would like to purchase a reference like Plauger's...
132
by: Frederick Gotham | last post by:
If we look at a programming language such as C++: When an updated Standard comes out, everyone adopts it and abandons the previous one. It seems though that things aren't so clear-cut in the C...
16
by: subramanian100in | last post by:
Program 1: --------------- #include <cstdlib> #include <iostream> #include <vector> using namespace std; int main() {
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.