Thread: it is possible to prevent implicit conversation in non-ctor-functions?

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    519

    it is possible to prevent implicit conversation in non-ctor-functions?

    Hi,

    consider a function taking a unsigned for example:

    Code:
    void func(unsigned _u)
    {}
    
    int main()
    {
         func(-200); // gives garbage inside func
    }
    I know explicit decleration, which unfortunately only works for ctors using one argument.
    Is there some else possiblitity besides configuring the compiler with something like "treat warnings as errors"?
    And what is the reason explicit is not defined for ctors with more than one argument or other functions?

    Thank You!

  2. #2
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Why wouldn't you always compile with the highest warning levels? Compiler warnings are your friend; if you ignore them, then whatever follows is your own fault.
    But I do think C++ should allow the explicit keyword in every function, not just constructors. I hope they add that feature soon.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    I do compile with all warnings I can get. For now I'm working on some library to give away and I want to help the users as much as I can to not run into slips.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    With all due respect, if they pass a signed integer into one where unsigned is expected, they're bound to run into problems. And it might also help to specify actual type, such as unsigned int.
    From a language standpoint, unless you just typeid, there's no way to determine if someone has passed a correct argument and, indeed, restrict it.
    And there's an option in most compilers that treats warnings as errors and refuses to compile, but I don't find it too big an issue for C++. I use it all the time with C, but not C++...
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Elysia View Post
    And there's an option in most compilers that treats warnings as errors and refuses to compile, but I don't find it too big an issue for C++. I use it all the time with C, but not C++...
    I use Warning Level 4 & Treat warnings as Errors all the time.
    If you don't Treat Warnings as Errors, you'll get warnings the first time you compile, then if you click build again it doesn't report those warnings anymore until you do a Rebuild All.
    Plus it forces lazy developers on your team to fix ALL warnings before they can create the executable.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    What brings me to a releated question:
    Do you check your build scripts/make files/solutions/whatever (with warning setup inside) into the VCS?

    I do, but some guys beliefs into other compilers and ignores them.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I always heed warnings unless I'm messing around with some non-serious code (like testing) and can probably leave unused variables in and such. That's also the primary reason I don't treat warnings like errors. If I don't need to fix them, then I don't.
    But when doing serious coding, I always pay attention to them and fix them, as opposed to ignoring them.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by pheres View Post
    What brings me to a releated question:
    Do you check your build scripts/make files/solutions/whatever (with warning setup inside) into the VCS?

    I do, but some guys beliefs into other compilers and ignores them.
    Yes, at my last company they created a /src directory for all the .h & .cpp files, then created a /x86-vc6 directory for VC6 Project & Workspace files, /x86-vc8 for VC++ 2005 files, /unix directory for makefiles...

    My new company throws everything into one directory which is quite annoying.
    All that gets checked into CVS, Source Safe, Perforce or whatever.

  9. #9
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I check my jamfiles in.

    You cannot prevent the compiler from performing the built-in implicit conversions. It's an important criticism of C and C++.
    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

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    519
    What brings me to a releated question:

    If it's not tolerated inside the C++ forum, maybe it can be split off and moved to the tech board.

    What build system do you like most and for what main reason? Has anyone experiences with SCONS?

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Please ask the build tool question in the Tech board. It is more general rather than C++ specific.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by Elysia View Post
    And it might also help to specify actual type, such as unsigned int.
    unsigned is identical to unsigned int. the int is implied.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Perhaps, but I always find it better to be explicit, just like return int from main.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 10-24-2007, 08:10 AM
  2. implicit declaration of function?
    By BoneXXX in forum C Programming
    Replies: 2
    Last Post: 04-27-2007, 11:04 PM
  3. Implicit declaration of function
    By soothsayer in forum C Programming
    Replies: 5
    Last Post: 02-28-2006, 02:56 PM
  4. Implicit and Explicit
    By ripper079 in forum C++ Programming
    Replies: 2
    Last Post: 09-06-2002, 12:22 PM
  5. implicit declaration of malloc
    By cheezwhiz in forum C Programming
    Replies: 6
    Last Post: 01-13-2002, 01:17 PM