This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: possible bug in g++


On 15 February 2012 22:17, Mattia Jona-Lasinio wrote:
> Hi Jonathan,
>
> I don´t think this link applies. It is clear that linking will fail in
> the link examples because the static variables are declared but never
> defined,

Right, just like your code.

> i.e. they are never instantiated.

No, it's not to do with instantiation.

It's the same problem, your program is invalid and can be fixed by
adding definitions of the variables.

> On the other hand the difference between the non working versions
>
> std::cout << (b = aa< X<4> >(3))[1].v << std::endl;

The operator<< overload used here takes its argument by reference.
Binding a reference to an object requires the object to have an
address, so you need to provide a definition.

Read the FAQ more carefully.  Your program is invalid. The compiler is
not required to issue a diagnostic, so it *might* accept it, as in
your final example, but that doesn't mean the code is correct. The
code is still wrong.

You can fix it by adding a definition:

template<int T> const int X<T>::v;


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]