Alexander Stippler wrote in news:3f******@news.uni-ulm.de:
I do not really know why the following piece of code
works and what happens internally:
#include <iostream>
template <class T>
class ReferenceTest
{
public:
ReferenceTest()
: zero(0), value(*zero)
{
std::cout << "constructor works ?!?" <<
std::endl;
}
T *zero;
T &value;
};
int
main()
{
ReferenceTest<double> r;
return 0;
}
I would expect a segmentation fault when value gets initialized
in the constructor since *zero (which is *0 IMO) tries to access
a value at address 0. Where am I wrong
Nope, "segmentation fault" isn't defined by the standard. What
you have here is undefined behaviour, you broke the rules so
you shouldn't expect there to be any rules that say what your
code does.
Also there is no requirment that an "access at address ..." occurs
during initialization of a reference, though that doesn't mean a
conforming implementation *doesn't* access (or something similar) the
reference. It's all up to the implementation and it doesn't have to
do anything special at all (i.e. it can assume your not going to
write code that exibits undefined behaviour), further more it doesn't
have to document what it does (or doesn't) do.
If your implementation gives "segmentation fault's" or "core dumps"
then you could get one (if thats what you want) by adding
double d = r.value; to main().
HTH
Rob.
--
http://www.victim-prime.dsl.pipex.com/