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++ / April 2006

Tip: Looking for answers? Try searching our database.

Storing a ValueType inside an STL map?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Whitney Kew - 31 Mar 2006 03:30 GMT
Hello,

I'm using Managed C++ in .NET 2003.  I'm trying to map integers to Enums
inside an STL map container, but I cannot get it to compile.  This might be a
simple question, and I'm sure I'm missing something, but I haven't used STL a
lot inside MC++.  Here's my code:

#include <map>
#include <vcclr.h>

public __gc class Managed
{
public:
  __value enum MyEnum
  {
     Value1,
     Value2,
     Value3
  };

  typedef std::map<int, gcroot<MyEnum*> > MyMap;
  static MyMap* _myMap = new MyMap();

  Managed()
  {
     _myMap->insert(MyMap::value_type(123, __box(MyEnum::Value1)));
  }
};

int _tmain()
{
  return 0;
}

The compiler errors I'm getting are as follows:

C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\gcroot.h(61)
: error C2665: 'System::Runtime::InteropServices::GCHandle::Alloc' : none of
the 2 overloads can convert parameter 1 from type 'Managed::MyEnum __gc *'
       StlMapWithMgdEnum.cpp(0): could be
'System::Runtime::InteropServices::GCHandle
System::Runtime::InteropServices::GCHandle::Alloc(System::Object __gc *)'
       while trying to match the argument list '(Managed::MyEnum __gc *)'
       C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\include\gcroot.h(60) : while compiling class-template member
function 'gcroot<T>::gcroot(Managed::MyEnum __gc *)'
       with
       [
           T=Managed::MyEnum __gc *
       ]
       C:\Program Files\Microsoft Visual Studio .NET
2003\Vc7\include\utility(51) : see reference to class template instantiation
'gcroot<T>' being compiled
       with
       [
           T=Managed::MyEnum __gc *
       ]
       StlMapWithMgdEnum.cpp(19) : see reference to class template
instantiation 'std::pair<_Ty1,_Ty2>' being compiled
       with
       [
           _Ty1=const int,
           _Ty2=gcroot<Managed::MyEnum __gc *>
       ]
C:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\gcroot.h(63)
: error C2228: left of '.ToInt32' must have class/struct/union type

Is it possible to do what I'm attempting here?  I want to use std::map for
the type safety, but do I need to punt and resort to using a
System::Collections::Hashtable and __box everything?

Thanks!
Signature

Whitney Kew
Software Engineer

"Gary Chang[MSFT]" - 31 Mar 2006 10:47 GMT
Hi Whitney,

Thank you posting!

It seems the errors are caused by the following code:

> typedef std::map<int, gcroot<MyEnum*> > MyMap;

The gcroot<T> template needs the type T is a managed pointer type which
points to a  __gc type object. Since your MyEnum is a value type object,
you need to inbox its pointer before using the gcroot<T> template to wrap
it:

..
  __value enum MyEnum
  {
     Value1,
     Value2,
     Value3
  };

  typedef __box MyEnum* pMyEnum;

  typedef std::map<int, gcroot<pMyEnum> > MyMap;
..

Wish the above sample code helps, thanks!

Best regards,

Gary Chang
Microsoft Community Support
======================================================
PLEASE NOTE the newsgroup SECURE CODE and PASSWORD will be updated at 9:00
AM PST, February 14, 2006.  Please complete a re-registration process by
entering the secure code mmpng06 when prompted. Once you have entered the
secure code mmpng06, you will be able to update your profile and access the
partner newsgroups.
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Whitney Kew - 31 Mar 2006 19:02 GMT
Thank you Gary, that solved my problem!
Signature

Whitney Kew
Software Engineer

> Hi Whitney,
>
[quoted text clipped - 39 lines]
> ==================================================
> This posting is provided "AS IS" with no warranties, and confers no rights.
"Gary Chang[MSFT]" - 03 Apr 2006 06:51 GMT
You are welcome, Whitney. :)

Have a nice day!

Best regards,

Gary Chang
Microsoft Community Support
======================================================
PLEASE NOTE the newsgroup SECURE CODE and PASSWORD will be updated at 9:00
AM PST, February 14, 2006.  Please complete a re-registration process by
entering the secure code mmpng06 when prompted. Once you have entered the
secure code mmpng06, you will be able to update your profile and access the
partner newsgroups.
==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
Holger Grund - 03 Apr 2006 15:31 GMT
>   __value enum MyEnum
>   {
[quoted text clipped - 4 lines]
>
>   typedef std::map<int, gcroot<MyEnum*> > MyMap;

You do not need gcroot here. You can the boxed version of the value
type. This is almost always pure overhead. Value types can perfectly
exist outside the garbage-collected heap (that's actually why they exist).

Hence, it is perfectly valid to write
std::map<int,MyEnum>

>      _myMap->insert(MyMap::value_type(123, __box(MyEnum::Value1)));
which would, of course, also mean that you would remove __box()

-hg

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.