Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / August 2006

Tip: Looking for answers? Try searching our database.

C++/CLI issue: using STL and CLI value types (C# structs)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
John - 27 Aug 2006 17:24 GMT
I'm having a major problem trying to use value types like
System::Drawing::Rectangle with std::vector.  Is it possible to use STL
containers with these type of objects, or am I just doing something wrong?

Thanks!

[ -- code snippet -- ]
using namespace System::Drawing;
#include <vector>

public ref class Base
{
    static void GetQuads( Base^ root )
    {
        std::vector<Rectangle> q;
    }
};

gives this error

C:\Program Files\Microsoft Visual Studio 8\VC\include\xutility(1290) :
error C4439: 'std::_Ptr_cat' : function definition with a managed type
in the signature must have a __clrcall calling convention
2>        C:\Program Files\Microsoft Visual Studio
8\VC\include\xmemory(226) : see reference to function template
instantiation 'std::_Nonscalar_ptr_iterator_tag
std::_Ptr_cat<_Ty*,_Ty*>(_T1 &,_T2 &)' being compiled
2>        with
2>        [
2>            _Ty=System::Drawing::Rectangle,
2>            _T1=System::Drawing::Rectangle *,
2>            _T2=System::Drawing::Rectangle *
2>        ]
2>        C:\Program Files\Microsoft Visual Studio
8\VC\include\vector(1083) : see reference to function template
instantiation 'void
std::_Destroy_range<System::Drawing::Rectangle,std::allocator<_Ty>>(_Ty
*,_Ty *,_Alloc &)' being compiled
2>        with
2>        [
2>            _Ty=System::Drawing::Rectangle,
2>            _Alloc=std::allocator<System::Drawing::Rectangle>
2>        ]
2>        C:\Program Files\Microsoft Visual Studio
8\VC\include\vector(1082) : while compiling class template member
function 'void std::vector<_Ty>::_Destroy(System::Drawing::Rectangle
*,System::Drawing::Rectangle *)'
2>        with
2>        [
2>            _Ty=System::Drawing::Rectangle
2>        ]
2>        c:\depot-jmatzen-mouse\depot\game14\src\engine\ui\Base.h(145)
: see reference to class template instantiation 'std::vector<_Ty>' being
compiled
2>        with
2>        [
2>            _Ty=System::Drawing::Rectangle
2>        ]
Carl Daniel [VC++ MVP] - 27 Aug 2006 21:01 GMT
> I'm having a major problem trying to use value types like
> System::Drawing::Rectangle with std::vector.  Is it possible to use
> STL containers with these type of objects, or am I just doing
> something wrong?

No, you're not doing anything wrong - it justs doesn't work.

There's a feature known as STL/CLR that was intended to be included in VC
2005 but they weren't able to get it done in time.  When it's eventually
released, it'll directly address the kind of thing you're trying to do.  In
the meantime, you have to settle with using framework containers for
framework objects.

See

http://msdn.microsoft.com/msdnmag/issues/06/04/PureC/default.aspx

for some background on STL/CLR. AFIAK, no release date for STL/CLR has been
announced

-cd
Holger Grund - 28 Aug 2006 10:24 GMT
>> I'm having a major problem trying to use value types like
>> System::Drawing::Rectangle with std::vector.  Is it possible to use
>> STL containers with these type of objects, or am I just doing
>> something wrong?
>
> No, you're not doing anything wrong - it justs doesn't work.

The particular diagnostic sounds rather academic. Did you try to
downgrade/disable the error? I guess that won't work because
vector will use placement new.

Anyway for Rectangle, this might be used in COM Interop
and therefore should have sequential layout, which should
mean it's binary compatible with the corresponding native
type (RECT). So just using std::vector<RECT> with some
conversions should work just fine.

-hg
Semmel - 28 Aug 2006 12:22 GMT
> I'm having a major problem trying to use value types like
> System::Drawing::Rectangle with std::vector.  Is it possible to use STL
> containers with these type of objects, or am I just doing something wrong?

Hi John,
try storing gcroot or auto_gcroot pointers of the managed objects in
order to embed them within native types like std:vector:

std::vector<gcroot<Rectangle^>> q;

See "Mix Types Safely and Correctly" in
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnvs05/html/Cpl
usCLIBP.asp


Semmel
Tamas Demjen - 28 Aug 2006 19:07 GMT
> try storing gcroot or auto_gcroot pointers of the managed objects in
> order to embed them within native types like std:vector:
>
> std::vector<gcroot<Rectangle^>> q;

A possible problem with this is that Rectangle becomes a GC type, which
means a hidden wrapper is created around it. This is called boxing, and
it's done implicitly. I'd consider using List<Rectangle>, and gcrooting
that if needed.

If gcroot is used, it requires a manual call to delete, as gcroot is
like a naked pointer. auto_gcroot is the exception safe version, but I'm
not sure if it can be stored in a container safely. It could be like
auto_ptr, which doesn't mix with STL.

Tom

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.