Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>*

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alex Vinokur

    #1

    Casting from const pair<const unsigned char*, size_t>* to constpair<unsigned char*, size_t>*

    Hi,

    Is it possible to do C++-casting from
    const pair<const unsigned char*, size_t>*
    to
    const pair<unsigned char*, size_t>*
    ?

    Alex Vinokur
  • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

    #2
    Re: Casting from const pair&lt;const unsigned char*, size_t&gt;* to constpair&lt;un signed char*, size_t&gt;*

    On 2008-10-12 13:16, Alex Vinokur wrote:
    Hi,
    >
    Is it possible to do C++-casting from
    const pair<const unsigned char*, size_t>*
    to
    const pair<unsigned char*, size_t>*
    ?
    const_cast might work, but I don't think this counts as casting away
    constness, you should probably use reinterpret_cas t.

    --
    Erik Wikström

    Comment

    • Barry

      #3
      Re: Casting from const pair&lt;const unsigned char*, size_t&gt;* to constpair&lt;un signed char*, size_t&gt;*

      On Oct 12, 7:16 pm, Alex Vinokur <ale...@users.s ourceforge.netw rote:
      Hi,
      >
      Is it possible to do C++-casting from
      const pair<const unsigned char*, size_t>*
      to
      const pair<unsigned char*, size_t>*
      ?
      >
      const pair<const unsigned char*, size_t>* p1 = 0;
      const pair<unsigned char*, size_t>* p2 =
      reinterpret_cas t<const pair<unsigned char*, size_t>*>(p1);

      but reinterpre_cast should be avoided if possible.
      *Practically*, I wonder you can just do it this way:

      char* s = const_cast<char *>(p1->first);

      Or can you tell me the scenario you are in?

      --
      Best Regards
      Barry

      Comment

      • Alex Vinokur

        #4
        Re: Casting from const pair&lt;const unsigned char*, size_t&gt;* to constpair&lt;un signed char*, size_t&gt;*

        On Oct 12, 2:22 pm, Barry <dhb2...@gmail. comwrote:
        On Oct 12, 7:16 pm, Alex Vinokur <ale...@users.s ourceforge.netw rote:
        >
        Hi,
        >
        Is it possible to do C++-casting from
        const pair<const unsigned char*, size_t>*
        to
        const pair<unsigned char*, size_t>*
        ?
        >
          const pair<const unsigned char*, size_t>* p1 = 0;
          const pair<unsigned char*, size_t>* p2 =
                reinterpret_cas t<const pair<unsigned char*, size_t>*>(p1);
        >
        but reinterpre_cast should be avoided if possible.
        *Practically*, I wonder you can just do it this way:
        >
          char* s = const_cast<char *>(p1->first);
        >
        Or can you tell me the scenario you are in?
        >
        I have function foo1 (const pair<unsigned char*, size_t>* p);
        I need also function foo2 (const pair<const unsigned char*, size_t>*
        p) that does the same thing as foo1().

        Currently
        void foo2 (const pair<const unsigned char*, size_t>* p)
        {
        // I would like to use here C++-style casting
        const pair<unsigned char*, size_t>* p1 = ( const pair<unsigned
        char*, size_t>* ) p;
        foo1(p1);
        }

        Alex Vinokur

        Comment

        • Sam

          #5
          Re: Casting from const pair&lt;const unsigned char*, size_t&gt;* to constpair&lt;un signed char*, size_t&gt;*

          Alex Vinokur writes:
          Hi,
          >
          Is it possible to do C++-casting from
          const pair<const unsigned char*, size_t>*
          to
          const pair<unsigned char*, size_t>*
          ?
          Yes, if you write your own conversion function. Although, if you are
          attempting to do something like this, then it's fairly likely that whatever
          you're really trying to do, you're doing it the wrong way.



          -----BEGIN PGP SIGNATURE-----
          Version: GnuPG v1.4.9 (GNU/Linux)

          iEYEABECAAYFAkj x9tEACgkQx9p3GY HlUOKbtQCdHLZUG km+k5aHKdQtEiQt cc3o
          vaMAniIYUdDc0XE sfWITCcbqSe9GLs me
          =goWJ
          -----END PGP SIGNATURE-----

          Comment

          • Barry

            #6
            Re: Casting from const pair&lt;const unsigned char*, size_t&gt;* to constpair&lt;un signed char*, size_t&gt;*

            On Oct 12, 9:05 pm, Alex Vinokur <ale...@users.s ourceforge.netw rote:
            On Oct 12, 2:22 pm, Barry <dhb2...@gmail. comwrote:
            >
            >
            >
            On Oct 12, 7:16 pm, Alex Vinokur <ale...@users.s ourceforge.netw rote:
            >
            Hi,
            >
            Is it possible to do C++-casting from
            const pair<const unsigned char*, size_t>*
            to
            const pair<unsigned char*, size_t>*
            ?
            >
              const pair<const unsigned char*, size_t>* p1 = 0;
              const pair<unsigned char*, size_t>* p2 =
                    reinterpret_cas t<const pair<unsigned char*, size_t>*>(p1);
            >
            but reinterpre_cast should be avoided if possible.
            *Practically*, I wonder you can just do it this way:
            >
              char* s = const_cast<char *>(p1->first);
            >
            Or can you tell me the scenario you are in?
            >
            I have function foo1 (const pair<unsigned char*, size_t>* p);
            I need also function foo2 (const pair<const unsigned char*, size_t>*
            p) that does the same thing as foo1().
            >
            Currently
            void foo2 (const pair<const unsigned char*, size_t>* p)
            {
              // I would like to use here C++-style casting
              const pair<unsigned char*, size_t>* p1 = ( const pair<unsigned
            char*, size_t>* ) p;
              foo1(p1);
            >
            }
            >
            I'm sorry that my previous post misled you.
            I think sam got my answer.

            What I asked is that I was confused that why you need
            such conversion. And I was expecting to see if there's some
            way to avoid such conversion.

            If you just wanted to learn the language. OK, "reinterpret_ca st"
            as C++-style cast, or just use C-style cast. While in practice,
            avoid doing this.

            --
            Best Regards
            Barry

            Comment

            • peter koch

              #7
              Re: Casting from const pair&lt;const unsigned char*, size_t&gt;* to constpair&lt;un signed char*, size_t&gt;*

              On 12 Okt., 13:16, Alex Vinokur <ale...@users.s ourceforge.netw rote:
              Hi,
              >
              Is it possible to do C++-casting from
              const pair<const unsigned char*, size_t>*
              to
              const pair<unsigned char*, size_t>*
              ?
              >
              Alex Vinokur
              Only a reinterpret_cas t works: the two types are unrelated. Why do you
              want to do this? It would be better to fix your design instead.

              /Peter

              Comment

              • James Kanze

                #8
                Re: Casting from const pair&lt;const unsigned char*, size_t&gt;* to constpair&lt;un signed char*, size_t&gt;*

                On Oct 12, 4:08 pm, peter koch <peter.koch.lar ...@gmail.comwr ote:
                On 12 Okt., 13:16, Alex Vinokur <ale...@users.s ourceforge.netw rote:
                Is it possible to do C++-casting from
                const pair<const unsigned char*, size_t>*
                to
                const pair<unsigned char*, size_t>*
                ?
                Only a reinterpret_cas t works: the two types are unrelated.
                Why do you want to do this? It would be better to fix your
                design instead.
                Reinterpret_cas t doesn't work. You can't do a reinterpret_cas t
                to or from a user defined type, and instantiations of std::pair
                are considered user defined types.

                What he can do is construct a new std::pair, e.g.:

                std::pair< unsigned char*, size_t const p2
                = std::make_pair(
                const_cast< unsigned char* >( p1.first ),
                p2.second ) ;

                --
                James Kanze (GABI Software) email:james.kan ze@gmail.com
                Conseils en informatique orientée objet/
                Beratung in objektorientier ter Datenverarbeitu ng
                9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                Comment

                • Vidar Hasfjord

                  #9
                  Re: Casting from const pair&lt;const unsigned char*, size_t&gt;* to constpair&lt;un signed char*, size_t&gt;*

                  On Oct 12, 2:05 pm, Alex Vinokur <ale...@users.s ourceforge.netw rote:
                  I have function foo1 (const pair<unsigned char*, size_t>* p);
                  I need also function foo2 (const pair<const unsigned char*, size_t>*
                  p) that does the same thing as foo1().
                  template <class T>
                  void foo_impl (const pair <T, size_t>* p) {...}

                  typedef pair <const unsigned char*, size_tpair1;
                  typedef pair <unsigned char*, size_tpair2;

                  void foo1 (const pair1* p) {foo_impl (p);}
                  void foo2 (const pair2* p) {foo_impl (p);}

                  Regards,
                  Vidar Hasfjord

                  Comment

                  • =?UTF-8?B?RXJpayBXaWtzdHLDtm0=?=

                    #10
                    Re: Casting from const pair&lt;const unsigned char*, size_t&gt;* to constpair&lt;un signed char*, size_t&gt;*

                    On 2008-10-13 11:27, James Kanze wrote:
                    On Oct 12, 4:08 pm, peter koch <peter.koch.lar ...@gmail.comwr ote:
                    >On 12 Okt., 13:16, Alex Vinokur <ale...@users.s ourceforge.netw rote:
                    Is it possible to do C++-casting from
                    const pair<const unsigned char*, size_t>*
                    to
                    const pair<unsigned char*, size_t>*
                    ?
                    >
                    >Only a reinterpret_cas t works: the two types are unrelated.
                    >Why do you want to do this? It would be better to fix your
                    >design instead.
                    >
                    Reinterpret_cas t doesn't work. You can't do a reinterpret_cas t
                    to or from a user defined type, and instantiations of std::pair
                    are considered user defined types.
                    He wanted to cast from a pointer to a user defined type to a pointer to
                    another user defined type, which is allowed.

                    --
                    Erik Wikström

                    Comment

                    • James Kanze

                      #11
                      Re: Casting from const pair&lt;const unsigned char*, size_t&gt;* to constpair&lt;un signed char*, size_t&gt;*

                      On Oct 13, 5:48 pm, Erik Wikström <Erik-wikst...@telia. comwrote:
                      On 2008-10-13 11:27, James Kanze wrote:
                      >
                      On Oct 12, 4:08 pm, peter koch <peter.koch.lar ...@gmail.comwr ote:
                      On 12 Okt., 13:16, Alex Vinokur <ale...@users.s ourceforge.netw rote:
                      Is it possible to do C++-casting from
                      const pair<const unsigned char*, size_t>*
                      to
                      const pair<unsigned char*, size_t>*
                      ?
                      Only a reinterpret_cas t works: the two types are unrelated.
                      Why do you want to do this? It would be better to fix your
                      design instead.
                      Reinterpret_cas t doesn't work. You can't do a reinterpret_cas t
                      to or from a user defined type, and instantiations of std::pair
                      are considered user defined types.
                      He wanted to cast from a pointer to a user defined type to a
                      pointer to another user defined type, which is allowed.
                      Yep. I missed the trailing * in the original posting. In that
                      case, you can use reinterpret_cas t to replace a compile time
                      error with runtime undefined behavior. Otherwise, you do need
                      to create a new object, using the technique I described.

                      (Also, it seems sort of strange to have pointers to an
                      std::pair. The object definitely has value semantics.)

                      --
                      James Kanze (GABI Software) email:james.kan ze@gmail.com
                      Conseils en informatique orientée objet/
                      Beratung in objektorientier ter Datenverarbeitu ng
                      9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

                      Comment

                      Working...