c++ generic struct

Why wont this compile

template <class t>
struct a
{
a(t& ty=t() ,a* p=0):o(ty),n(p){}
t& o;
a* n;


};
What are the errors?
Correction: It WILL compile.
If it doesn't compile for you, then you must be getting errors somewhere, and I doubt it is with this piece of code (unless your compiler is out of date).
A temporary object can't bind to a non-const reference.
Last edited on
It works in Visual Studio.
It works in Visual Studio.


If your definition of "works" is "the compiler doesn't complain if I don't instantiate an instance," then yes, it "works."

1
2
3
4
5
6
7
8
9
10
11
12
template <class t>
struct a
{
a(t& ty=t() ,a* p=0):o(ty),n(p){}
t& o;
a* n;
}; 

int main()
{
    a<int> c ;
}


No. My definition of works is the program compiles and then runs with the expected results. I'm not sure if you consider both these definitions to mean the same thing, but that's what I mean by it works.
Then you may want to find a version of VS that isn't broken.

Tested: The snippet in my post doesn't compile in VS2008, VS2010 or VS2012.
Last edited on
Topic archived. No new replies allowed.