Thread: static_cast

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    static_cast

    Why isnt this the same:

    Code:
    time_t now = time (0);
    unsigned char *p = (unsigned char*)&now;
    as:

    Code:
    time_t now = time (0);
    unsigned char *p = static_cast<unsigned char*> &now;

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    You are missing the parenthesis around &now
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    630
    I ger error:

    error C2440: 'static_cast' : cannot convert from 'time_t *__w64 ' to 'unsigned char *'

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Why are you trying to do that? What are you trying to accomplish?

  5. #5
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    No. You can't do that conversion. Think of static_cast as the explicit request of a cast that the compiler could under the right conditions perform implicitly (there's a few exceptions like casts from void*).

    The compiler cannot, and will not, perform a static_cast between those two types. time_t is defined as some sort of integral type (probably a long integer). You are requesting a conversion between pointer to an integral type and pointer to char.

    You could probably convert from time_t to char with a necessary loss of data, but what you are requesting is a conversion between pointers. There's no such static_cast unless one of the pointers in question is a void*.

    You could do that cast with a reinterpret cast. But the results are probably not what you are trying to do.

    I believe you are trying to store the current time in char[] format. Being that the case, what you probably want is this:

    Code:
    char* currtime = asctime(localtime(time()));
    time() returns a time_t representation of the current system time.
    localtime() returns a tm struct constructed from a time_t object.
    asctime() returns a string representation of the tm struct.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >You could do that cast with a reinterpret cast. But the results are probably not what you are trying to do.
    I'd wager it's exactly what he's trying to do, as long as he's trying to do the same thing as the code he got that snippet from. It looks suspiciously like something I wrote that puns the value of a time_t into a sequence of bytes for hashing.
    My best code is written with the delete key.

  7. #7
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Well... on that case he will definitely need reinterpret_cast.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

Popular pages Recent additions subscribe to a feed