Thread: automatic type conversation style question

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    88

    automatic type conversation style question

    I think automatic (implicit) type conversation are bad and want to use explicit casts only. Is this a good style?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Not necessarily. Implicit casts are designed to make your life easier, and if you use explicit casts all of the time, your code can become verbose to the point of unreadability. On the other hand, some confusing situations can be made easier to read with an explicit cast.

    For example, consider some code that calls sqrt() on an integer. Do you really need a cast here?
    Code:
    double n = sqrt(9);
    I usually don't put casts in place when up-casting is involved; for example, an int being promoted to a double. But I consider it good style to put a cast in place for the opposite scenario, and your compiler probably does too if you turn up its warning level.

    In the end it's your choice. Just be aware that using casts all of the time can quickly make code unreadable. sqrt(9) is readable enough as it is.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    In that particular example, I would have replaced "9" with "9." to avoid the type conversion. On the other hand, if the argument is an integer i, that won't work, so the point holds. Although in C++, sqrt() is overloaded to take and return either float, double, or long double, so an explicit cast might eliminate confusion about which is being used. Do you know if it uses the double version if an integer argument is passed to it?

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I also really wouldn't use a cast operator for up-casts in a class hierarchy. I think, in fact, that it would be a bad thing to do.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using VC Toolkit 2003
    By Noobwaker in forum Windows Programming
    Replies: 8
    Last Post: 03-13-2006, 07:33 AM
  2. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  3. qwerty/azerty keyboard type problem + question about loop.
    By Robin Hood in forum C++ Programming
    Replies: 9
    Last Post: 07-22-2002, 01:03 PM
  4. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  5. Newbies question about the integer type
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-17-2001, 08:09 PM