[try Beta version]
Not logged in

 
c++ generic struct

Dec 2, 2012 at 11:38pm
Why wont this compile

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


};
Dec 2, 2012 at 11:47pm
What are the errors?
Dec 3, 2012 at 12:13am
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).
Dec 3, 2012 at 12:56am
A temporary object can't bind to a non-const reference.
Last edited on Dec 3, 2012 at 12:58am
Dec 3, 2012 at 5:09am
It works in Visual Studio.
Dec 3, 2012 at 6:15am
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 ;
}


Dec 18, 2012 at 3:05am
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.
Dec 18, 2012 at 3:16am
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 Dec 18, 2012 at 3:16am
Topic archived. No new replies allowed.