Why is this class declaration not working in Visual Studio

So, I'm trying to get code written to compile gcc on Visual Studio 2008. I have a problem that I narrowed down to this:

class value_t { public: typedef std::deque<value_t> sequence_t; typedef sequence_t::iterator iterator; }; 

This code does not work:

 1>cpptest.cpp 1>c:\program files\microsoft visual studio 9.0\vc\include\deque(518) : error C2027: use of undefined type 'value_t' 1> c:\temp\cpptest\cpptest.cpp(10) : see declaration of 'value_t' 1> c:\temp\cpptest\cpptest.cpp(13) : see reference to class template instantiation 'std::deque<_Ty>' being compiled 1> with 1> [ 1> _Ty=value_t 1> ] 1>c:\program files\microsoft visual studio 9.0\vc\include\deque(518) : error C2027: use of undefined type 'value_t' 1> c:\temp\cpptest\cpptest.cpp(10) : see declaration of 'value_t' 

However, when I try to do this with std :: vector, it compiles fine:

 class value_t { public: typedef std::vector<value_t> sequence_t; typedef sequence_t::iterator iterator; }; 

What happened? I tried adding โ€œtypenameโ€ everywhere I can think of, but for now I think it's just a bug in the Dinkumware STL. Can someone explain what is happening and / or suggest a solution? Thanks.

+6
c ++ templates
source share
3 answers

His behavior is undefined. See this link to clC ++. Moderated

Snip from Daniel K answer: -

the C ++ standard (both C ++ 03 and C ++ 0x) says that you are trying to invoke undefined behavior, see [lib.res.on.functions] / 2:

"In particular, the effects are undefined in the following cases: [..] - if the incomplete type (3.9) is equal, it is used as an argument to the template when creating an instance of the template component.

+7
source share

I think the problem is that value_t is an incomplete type until you get to the end of the definition. Trying to use an incomplete type as a template parameter for a standard container should not actually work. In some cases, it can / will work, but if it did not go through with all standard types of containers, it still will not signal any error. The standard requires it to be a complete type, so if it is not, you get what you get - it probably should work in sequence, but if that happens, there is nothing wrong with that.

+5
source share

You are trying to use the class internally in the template. How is this allowed? I do not know that I have ever tried to do this, but is it possible? I do not know why this works for std :: vector, but I suppose this is wrong. You define a class and use this definition in the definition. Seems wrong. Good luck with this, I will be interested to see some deeper answers myself ...

+1
source share

All Articles