Near a solution

Discussion in 'C++' started by Tomás, May 28, 2006.

  1. Tomás

    Tomás Guest

    Tomás posted:

    > template<class T>
    > const uchar* ByteAdrs( const T* const p )
    > {
    > return reinterpret_cast<const uchar*>(p);
    > }
    >
    > template<class T>
    > unsigned uchar* ByteAdrs( T* const p )
    > {
    > return reinterpret_cast<uchar*>(p);
    > }



    Building upon some sample code that Alf P. Steinbach gave me, I've come
    up with the following code. It doesn't compile, but I think I'm pretty
    close to a solution.

    template<class T>
    struct DetermineConstness {

    /* Two types of differing size: */
    struct SizeRepresentModifiable { char c; };
    struct SizeRepresentConst { double array[50]; };

    static T &ReturnRef(void);

    static SizeRepresentModifiable TakesRef(T &);

    static SizeRepresentConst TakesRef(const T &);

    static bool const is_const =
    sizeof( TakesRef( ReturnRef() ) )
    == sizeof( SizeRepresentConst );
    };

    template<bool is_const, class T>
    struct ConstnessVersion
    {
    typedef T Type;
    };

    template<class T>
    struct ConstnessVersion<true, T>
    {
    typedef T const Type;
    };


    template<class T>
    typename ConstnessVersion<DetermineConstness<T>::is_const, char>::Type
    *ByteAdrs( T * const p )
    {
    return
    reinterpret_cast
    <typename ConstnessVersion<DetermineConstness<T>::is_const,
    char>::Type *>
    (p);
    }

    #include <string>
    using std::string;

    int main()
    {
    string str;
    string const cstr;

    const char *p1 = ByteAdrs(&str); /* No problem */
    const char *p2 = ByteAdrs(&cstr); /* No problem */

    char *p3 = ByteAdrs(&str); /* No problem */
    //char *p4 = ByteAdrs(&cstr); /* ERROR: const clash */
    }


    Any ideas? Once I get this to work I'll start back on trying to write a
    proper "implicit_cast".


    -Tomás
     
    Tomás, May 28, 2006
    #1
    1. Advertising

  2. Tomás

    kwikius Guest

    Tomás wrote:


    > Any ideas? Once I get this to work I'll start back on trying to write a
    > proper "implicit_cast".


    What is implicit_cast's purpose? FWIW there is a (currently
    undocumented but hopefully documented in next release) function named
    implicit_cast in the boost distro. Header is <boost/implicit_cast.hpp>

    regards
    Andy Little
     
    kwikius, May 28, 2006
    #2
    1. Advertising

  3. Tomás

    Tomás Guest

    kwikius posted:

    > FWIW there is a (currently
    > undocumented but hopefully documented in next release) function named
    > implicit_cast in the boost distro. Header is <boost/implicit_cast.hpp>



    I downloaded it just there and tried it out... it works perfectly! It's a
    stroke of genius! I had to look through the source code to figure out how
    it works, but unforunately it's flooded with macros so it would take quite
    a while.

    It seems that the "identity" template is the magic behind it.


    -Tomás
     
    Tomás, May 28, 2006
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?UGF0cmljay5PLklnZQ==?=

    System.Data.SqlClient.SqlException: Line 1: Incorrect syntax near

    =?Utf-8?B?UGF0cmljay5PLklnZQ==?=, Oct 11, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    10,776
    Patrick.O.Ige
    Oct 11, 2004
  2. Patrick Olurotimi Ige
    Replies:
    4
    Views:
    7,077
    Patrick Olurotimi Ige
    Mar 18, 2005
  3. JMUApache
    Replies:
    2
    Views:
    3,344
    Brendan Reynolds
    Sep 4, 2005
  4. David

    Incorrect syntax near 'int'

    David, Apr 6, 2006, in forum: ASP .Net
    Replies:
    7
    Views:
    2,513
    David
    Apr 7, 2006
  5. Peter Li
    Replies:
    0
    Views:
    409
    Peter Li
    May 27, 2004
Loading...

Share This Page