Trouble porting simple class from MEC++ to C++/CLI

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bern McCarty

    Trouble porting simple class from MEC++ to C++/CLI


    I have a simple ref class in its own namespace that needs to coexist with
    a legacy typedef alias for "unsigned int" in the global namespace that has
    the identifier as itself. Everything compiles fine with the old MEC++ syntax,
    but I cannot figure out how to write the code so that it will compile in
    C++/CLI. Can someone tell me how? Here is the code in both syntaxes:

    // This MEC++ code compiles just fine with VC8 using cl -c -clr:oldsyntax
    StaticClassOldS yntax.cpp

    typedef unsigned int LevelID;

    namespace Test
    {
    public __value class LevelID
    {
    public private: System::UInt32 m_id;

    public private: LevelID(System: :UInt32 const __gc&
    nlid);

    public: static ::LevelID op_Explicit (LevelID const lid);
    public: static LevelID op_Explicit (::LevelID const nlid);

    public: static bool op_Equality(Lev elID const l1, LevelID
    const l2);
    public: static bool op_Inequality(L evelID const l1, LevelID
    const l2);

    public: virtual bool Equals(System:: Object* obj);
    public: virtual int GetHashCode();
    public: virtual System::String* ToString();
    };

    public __abstract __sealed __gc class LevelIDs
    {
    public: static const ::Test::LevelID ApplicationInfo = 20u;
    };




    /* This fails to compile with VC8 using cl -c -clr StaticClass.cpp . It results
    in:

    Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.42
    for Microsoft (R) .NET Framework version 2.00.50727.42
    Copyright (C) Microsoft Corporation. All rights reserved.

    StaticClass.cpp
    StaticClass.cpp (27) : error C2061: syntax error : identifier 'LevelID'
    This diagnostic occurred in the compiler generated function 'void
    Test::LevelIDs: :LevelIDs(void) '

    */
    typedef unsigned int LevelID;

    namespace Test
    {
    public value class LevelID
    {
    internal: System::UInt32 m_id;

    internal: LevelID(System: :UInt32 const% nlid);

    public: static explicit operator ::LevelID(Level ID const lid);
    public: static explicit operator LevelID(::Level ID const nlid);

    public: static bool operator ==(LevelID const l1, LevelID const l2);
    public: static bool operator !=(LevelID const l1, LevelID const l2);

    public: virtual bool Equals(System:: Object ^ obj) override;
    public: virtual int GetHashCode() override;
    public: virtual System::String^ ToString() override;
    };


    public ref class LevelIDs abstract sealed
    {
    public: initonly static ::Test::LevelID ApplicationInfo = static_cast<::T est::LevelID>(2 0u);
    };

    }




  • Carl Daniel [VC++ MVP]

    #2
    Re: Trouble porting simple class from MEC++ to C++/CLI

    Bern McCarty wrote:
    I have a simple ref class in its own namespace that needs to coexist
    with a legacy typedef alias for "unsigned int" in the global namespace
    that has the identifier as itself. Everything compiles fine with the old
    MEC++ syntax, but I cannot figure out how to write the code so that it
    will compile
    in C++/CLI. Can someone tell me how? Here is the code in both syntaxes:
    >
    // This MEC++ code compiles just fine with VC8 using cl -c
    -clr:oldsyntax StaticClassOldS yntax.cpp
    >
    typedef unsigned int LevelID;
    >
    namespace Test
    {
    public __value class LevelID
    {
    public private: System::UInt32 m_id;
    >
    public private: LevelID(System: :UInt32 const
    __gc& nlid);
    >
    public: static ::LevelID op_Explicit (LevelID const
    lid); public: static LevelID op_Explicit (::LevelID
    const nlid);
    public: static bool op_Equality(Lev elID const l1,
    LevelID const l2);
    public: static bool op_Inequality(L evelID const
    l1, LevelID const l2);
    >
    public: virtual bool Equals(System:: Object* obj);
    public: virtual int GetHashCode();
    public: virtual System::String* ToString();
    };
    >
    public __abstract __sealed __gc class LevelIDs
    {
    public: static const ::Test::LevelID ApplicationInfo = 20u;
    };
    >
    >
    >
    >
    /* This fails to compile with VC8 using cl -c -clr StaticClass.cpp . It
    results in:
    >
    Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.42
    for Microsoft (R) .NET Framework version 2.00.50727.42
    Copyright (C) Microsoft Corporation. All rights reserved.
    >
    StaticClass.cpp
    StaticClass.cpp (27) : error C2061: syntax error : identifier
    'LevelID' This diagnostic occurred in the compiler
    generated function 'void Test::LevelIDs: :LevelIDs(void) '
    Have you tried explicitly defining the default constructor? Since that's
    where the compiler is getting tripped up, it seems like a good place to
    start.

    -cd



    Comment

    • Bern McCarty

      #3
      Re: Trouble porting simple class from MEC++ to C++/CLI


      But my class is a static class on purpose (i.e. it is both abstract and sealed).
      In such class you are not allowed to have any instance methods, including
      constructors. If you try to add one you'd get:

      'Test::LevelIDs ': a sealed abstract class cannot have any instance members

      So it is very bizarre that the compiler error message indicates that it is
      getting tripped up in a constructor that I do not have and that I am not
      allowed to have. I wonder if the error message is simply wrong and it is
      really referring to .cctor (a class constructor) ?

      At any rate, this is an awfully simple little compiland and I have no idea
      how to make it fly in C++/CLI. I'm surprised to have my syntax porting effort
      hung up on such simple things.

      -Bern

      Bern McCarty wrote:
      >
      >I have a simple ref class in its own namespace that needs to coexist
      >with a legacy typedef alias for "unsigned int" in the global
      >namespace
      >that has the identifier as itself. Everything compiles fine with the
      >old
      >MEC++ syntax, but I cannot figure out how to write the code so that
      >it
      >will compile
      >in C++/CLI. Can someone tell me how? Here is the code in both
      >syntaxes:
      >// This MEC++ code compiles just fine with VC8 using cl -c
      >-clr:oldsyntax StaticClassOldS yntax.cpp
      >>
      >typedef unsigned int LevelID;
      >>
      >namespace Test
      >{
      >public __value class LevelID
      >{
      >public private: System::UInt32 m_id;
      >public private: LevelID(System: :UInt32 const __gc&
      >nlid);
      >>
      >public: static ::LevelID op_Explicit (LevelID const
      >lid); public: static LevelID op_Explicit (::LevelID
      >const nlid);
      >public: static bool op_Equality(Lev elID const l1,
      >LevelID const l2);
      >public: static bool op_Inequality(L evelID const
      >l1, LevelID const l2);
      >public: virtual bool Equals(System:: Object* obj);
      >public: virtual int GetHashCode();
      >public: virtual System::String* ToString();
      >};
      >public __abstract __sealed __gc class LevelIDs
      >{
      >public: static const ::Test::LevelID ApplicationInfo = 20u;
      >};
      >/* This fails to compile with VC8 using cl -c -clr StaticClass.cpp .
      >It results in:
      >>
      >Microsoft (R) C/C++ Optimizing Compiler Version 14.00.50727.42 for
      >Microsoft (R) .NET Framework version 2.00.50727.42 Copyright (C)
      >Microsoft Corporation. All rights reserved.
      >>
      >StaticClass.cp p
      >StaticClass.cp p(27) : error C2061: syntax error : identifier
      >'LevelID' This diagnostic occurred in the compiler
      >generated function 'void Test::LevelIDs: :LevelIDs(void) '
      Have you tried explicitly defining the default constructor? Since
      that's where the compiler is getting tripped up, it seems like a good
      place to start.
      >
      -cd
      >

      Comment

      • Carl Daniel [VC++ MVP]

        #4
        Re: Trouble porting simple class from MEC++ to C++/CLI

        Bern McCarty wrote:
        But my class is a static class on purpose (i.e. it is both abstract
        and sealed). In such class you are not allowed to have any instance
        methods, including constructors. If you try to add one you'd get:
        >
        'Test::LevelIDs ': a sealed abstract class cannot have any instance
        members

        Sorry, I misred the error - I thought it was the constructor for LevelID
        that was the source of the error, not LevelIDs.
        >
        So it is very bizarre that the compiler error message indicates that
        it is getting tripped up in a constructor that I do not have and that
        I am not allowed to have. I wonder if the error message is simply
        wrong and it is really referring to .cctor (a class constructor) ?
        >
        At any rate, this is an awfully simple little compiland and I have no
        idea how to make it fly in C++/CLI. I'm surprised to have my syntax
        porting effort hung up on such simple things.
        I assume that if you get rid of the global typedef with the same name that
        the code compiles, but for whatever reason, you're stuck with having
        multiple definitions of this name?

        -cd


        Comment

        • Bern McCarty

          #5
          Re: Trouble porting simple class from MEC++ to C++/CLI


          Yes. I am stuck with both the LevelID alias in the global namespace and with
          reusing that identifier for the managed class in another namespace. If I
          can produce the class with MEC++, then I should be able to produce the same
          class with C++/CLI shouldn't I?

          Can anyone see how? The examples I provided are very small and easy to try
          out/tinker with.
          Bern McCarty wrote:
          >
          >But my class is a static class on purpose (i.e. it is both abstract
          >and sealed). In such class you are not allowed to have any instance
          >methods, including constructors. If you try to add one you'd get:
          >>
          >'Test::LevelID s': a sealed abstract class cannot have any instance
          >members
          >>
          Sorry, I misred the error - I thought it was the constructor for
          LevelID that was the source of the error, not LevelIDs.
          >
          >So it is very bizarre that the compiler error message indicates that
          >it is getting tripped up in a constructor that I do not have and that
          >I am not allowed to have. I wonder if the error message is simply
          >wrong and it is really referring to .cctor (a class constructor) ?
          >>
          >At any rate, this is an awfully simple little compiland and I have no
          >idea how to make it fly in C++/CLI. I'm surprised to have my syntax
          >porting effort hung up on such simple things.
          >>
          I assume that if you get rid of the global typedef with the same name
          that the code compiles, but for whatever reason, you're stuck with
          having multiple definitions of this name?
          >
          -cd
          >

          Comment

          • Gary Chang[MSFT]

            #6
            Re: Trouble porting simple class from MEC++ to C++/CLI

            Hi Bern,

            I already consulted this problem with our prodcut team, if there isn't any
            specific reason to use the static_cast, we suggest you can change the
            following line

            public: initonly static Test::LevelID ApplicationInfo =
            static_cast<Tes t::LevelID>(20u );

            to

            public: initonly static Test::LevelID ApplicationInfo =
            (Test::LevelID) (20u);


            Thanks!

            Best regards,

            Gary Chang
            Microsoft Online Community Support
            =============== =============== =============== =====
            Get notification to my posts through email? Please refer to
            Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.

            ications.
            =============== =============== =============== =====
            This posting is provided "AS IS" with no warranties, and confers no rights.

            Comment

            • Bern McCarty

              #7
              Re: Trouble porting simple class from MEC++ to C++/CLI


              Thanks Gary. I think that recommendation will get me by.

              This is a compiler bug it not? The LevelID class has explicit conversion
              operators that should used successfully here I think.
              Is this an acknowledged problem with the compiler or is it considered right
              and proper that reinterpret_cas t is necessary to produce this class?

              I find it very odd that I can produce this class with no cast operator whatsoever
              when using the old syntax but to produce the class in the new syntax I not
              only need to cast, but I need to get out the big gun: reinterpret_cas t.
              A mere static_cast is insufficient.

              -Bern
              Hi Bern,
              >
              I already consulted this problem with our prodcut team, if there isn't
              any specific reason to use the static_cast, we suggest you can change
              the following line
              >
              public: initonly static Test::LevelID ApplicationInfo =
              static_cast<Tes t::LevelID>(20u );
              >
              to
              >
              public: initonly static Test::LevelID ApplicationInfo =
              (Test::LevelID) (20u);
              >
              Thanks!
              >
              Best regards,
              >
              Gary Chang
              Microsoft Online Community Support
              =============== =============== =============== =====
              Get notification to my posts through email? Please refer to
              Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.

              #notif
              ications.
              =============== =============== =============== =====
              This posting is provided "AS IS" with no warranties, and confers no
              rights.

              Comment

              • Gary Chang[MSFT]

                #8
                Re: Trouble porting simple class from MEC++ to C++/CLI

                Thanks for the response, Bern.

                I am afraid this seems to be a compiler issue, the C++/CLI has many
                difference from the MEC++, so its complier behaves different from the
                VC2003. I will forward this issue to our corresponding product team for
                review.

                Good Luck!

                Best regards,

                Gary Chang
                Microsoft Online Community Support
                =============== =============== =============== =====
                Get notification to my posts through email? Please refer to
                Find official documentation, practical know-how, and expert guidance for builders working and troubleshooting in Microsoft products.

                ications.
                =============== =============== =============== =====
                This posting is provided "AS IS" with no warranties, and confers no rights.

                Comment

                • Ben Voigt

                  #9
                  Re: Trouble porting simple class from MEC++ to C++/CLI


                  "Bern McCarty" <Bern@newsgroup s.nospamwrote in message
                  news:5951ef0629 d198c8f17aa8f26 934@msnews.micr osoft.com...
                  >
                  Thanks Gary. I think that recommendation will get me by.
                  >
                  This is a compiler bug it not? The LevelID class has explicit conversion
                  operators that should used successfully here I think.
                  Is this an acknowledged problem with the compiler or is it considered
                  right and proper that reinterpret_cas t is necessary to produce this class?
                  I find it very odd that I can produce this class with no cast operator
                  whatsoever when using the old syntax but to produce the class in the new
                  syntax I not only need to cast, but I need to get out the big gun:
                  reinterpret_cas t. A mere static_cast is insufficient.
                  Can you use constructor notation?

                  public: initonly static Test::LevelID ApplicationInfo (20u);

                  >
                  -Bern
                  >
                  >Hi Bern,
                  >>
                  >I already consulted this problem with our prodcut team, if there isn't
                  >any specific reason to use the static_cast, we suggest you can change
                  >the following line
                  >>
                  >public: initonly static Test::LevelID ApplicationInfo =
                  >static_cast<Te st::LevelID>(20 u);
                  >>
                  >to
                  >>
                  >public: initonly static Test::LevelID ApplicationInfo =
                  >(Test::LevelID )(20u);
                  >>
                  >Thanks!
                  >>
                  >Best regards,
                  >>
                  >Gary Chang
                  >Microsoft Online Community Support
                  >============== =============== =============== ======
                  >Get notification to my posts through email? Please refer to
                  >http://msdn.microsoft.com/subscripti...s/default.aspx
                  >#notif
                  >ications.
                  >============== =============== =============== ======
                  >This posting is provided "AS IS" with no warranties, and confers no
                  >rights.
                  >
                  >

                  Comment

                  Working...