forcing a compile time error

Discussion in 'C++' started by Michael Gaab, Nov 8, 2003.

  1. Michael Gaab

    Michael Gaab Guest

    How would I force the compiler to throw an error for the following:

    function signature -
    void foo(short);

    function call -
    foo('d');

    My compiler does not complain when I call foo() with a character argument.
    I am assuming that the compiler does some type casting or that char's are
    interpreted as a byte. Not exactly sure.

    At any rate how do I force the compiler throw a error?

    thanks, Mike
     
    Michael Gaab, Nov 8, 2003
    #1
    1. Advertising

  2. > function signature -
    > void foo(short);
    >
    > function call -
    > foo('d');
    >
    > My compiler does not complain when I call foo() with a character argument.

    thats because a char(acter) is nothing more than a number (char from -128 to
    127 or unsigned char from 0 to 255)

    > I am assuming that the compiler does some type casting or that char's are
    > interpreted as a byte. Not exactly sure.

    there's no much difference to, say implicitly converting a short to a long

    > At any rate how do I force the compiler throw a error?

    nope, sorry

    regards,
    sev
     
    Severin Ecker, Nov 8, 2003
    #2
    1. Advertising

  3. Michael Gaab

    Jack Adam Guest

    Michael Gaab wrote:

    > function signature -
    > void foo(short);
    >
    > function call -
    > foo('d');
    >
    > My compiler does not complain when I call foo() with a character argument.
    > I am assuming that the compiler does some type casting or that char's are
    > interpreted as a byte. Not exactly sure.


    This is normal. There is an implicit type conversion from char to short
    and this is part of the C++ language, not some special compiler feature.

    If you want to have different behaviors for function foo depending on
    the exact type of the parameter, one solution is to to overload function
    foo.
     
    Jack Adam, Nov 8, 2003
    #3
  4. Michael Gaab

    lilburne Guest

    Michael Gaab wrote:
    > How would I force the compiler to throw an error for the following:
    >
    > function signature -
    > void foo(short);
    >
    > function call -
    > foo('d');
    >
    > My compiler does not complain when I call foo() with a character argument.
    > I am assuming that the compiler does some type casting or that char's are
    > interpreted as a byte. Not exactly sure.
    >
    > At any rate how do I force the compiler throw a error?
    >


    The normal way to force a compilation error is to use the
    #error preprocessor directive. Why you'd want to cause a
    compilation error specifically on foo though is a bit of a
    mystery but there are probably a myriad of ways of doing so

    foo('d')
    foo();
    foo(&d);
     
    lilburne, Nov 8, 2003
    #4
  5. "Jack Adam" <> wrote in message
    news:boj7tp$lck$...
    > Michael Gaab wrote:
    >
    > > function signature -
    > > void foo(short);
    > >
    > > function call -
    > > foo('d');
    > >
    > > My compiler does not complain when I call foo() with a character

    argument.
    > > I am assuming that the compiler does some type casting or that char's

    are
    > > interpreted as a byte. Not exactly sure.

    >
    > This is normal. There is an implicit type conversion from char to short
    > and this is part of the C++ language, not some special compiler feature.

    I am afraid you are wrong. There is not implicit type conversion from char
    to short in C++ language. There is the integral promotion from char to int
    (or unsigned int) that is not an integral conversion. So, as far as char is
    not an integer type, though signed char and unsigned char are integers, it
    shall be promoted to int and then converted (integral convertion) to short
    to make the foo(short) call.
    BTW that is why void foo(int); is better then void foo(short); for foo('d')
    call.

    --
    Michael Kochetkov.
     
    Michael Kochetkov, Nov 8, 2003
    #5
  6. Michael Gaab

    Jack Klein Guest

    On Sat, 8 Nov 2003 17:53:32 +0100, "Severin Ecker" <>
    wrote in comp.lang.c++:

    > > function signature -
    > > void foo(short);
    > >
    > > function call -
    > > foo('d');
    > >
    > > My compiler does not complain when I call foo() with a character argument.

    > thats because a char(acter) is nothing more than a number (char from -128 to
    > 127 or unsigned char from 0 to 255)


    No, a character is nothing more than a number between CHAR_MIN, which
    must be at least -127 if char is signed or 0 if char is unsigned, and
    CHAR_MAX which must be at least 127 if char is signed or 255 if char
    is unsigned. No C++ implementation is required to support a character
    with a value of -128, although some do.

    And on one compiler that I use a lot these days, a char can have any
    value between -32768 and +32767.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
     
    Jack Klein, Nov 8, 2003
    #6
  7. Michael Gaab

    Jack Adam Guest

    Michael Kochetkov wrote:

    >>This is normal. There is an implicit type conversion from char to short
    >> and this is part of the C++ language, not some special compiler feature.

    >
    > I am afraid you are wrong. There is not implicit type conversion from char
    > to short in C++ language. There is the integral promotion from char to int
    > (or unsigned int) that is not an integral conversion. So, as far as char is
    > not an integer type, though signed char and unsigned char are integers, it
    > shall be promoted to int and then converted (integral convertion) to short
    > to make the foo(short) call.



    I know. There is one word missing from my statement, which should read:

    "There is an implicit type conversion _sequence_ from char to short and
    this is part of the C++ language, not some special compiler feature."

    Thanks for your errata. Hope that's clear now.
     
    Jack Adam, Nov 8, 2003
    #7
  8. Michael Gaab wrote:
    > How would I force the compiler to throw an error for the following:
    >
    > function signature -
    > void foo(short);
    >
    > function call -
    > foo('d');
    >
    > My compiler does not complain when I call foo() with a character argument.
    > I am assuming that the compiler does some type casting or that char's are
    > interpreted as a byte. Not exactly sure.
    >
    > At any rate how do I force the compiler throw a error?


    One technique I use is to create a template and make a
    condition in the template to cause a compile time
    error when the offending condition is instantiated.


    void foo(short);

    inline void call_foo( short s )
    {
    foo( s );
    }

    template <typename T>
    inline void foo(T d)
    {
    struct compile_error_if_char
    {
    int array[ 1+ 1 / (sizeof(T)-1) ];
    } x;

    call_foo( d );
    }


    int main()
    {
    short x = 1;

    foo( x );

    foo('s');

    foo( 22 );
    }
     
    Gianni Mariani, Nov 8, 2003
    #8
  9. Michael Gaab

    NFish Guest

    Michael Gaab wrote:

    > How would I force the compiler to throw an error for the following:
    >
    > function signature -
    > void foo(short);
    >
    > function call -
    > foo('d');
    >
    > My compiler does not complain when I call foo() with a character argument.
    > I am assuming that the compiler does some type casting or that char's are
    > interpreted as a byte. Not exactly sure.
    >
    > At any rate how do I force the compiler throw a error?
    >
    > thanks, Mike


    ~) cat test.cpp
    void foo(short) {
    }

    #define foo(a) (1 / (sizeof(a) == sizeof(short)), (foo)(a))

    int main(void) {
    foo('d');
    return 0;
    }

    ~ ) c++ test.cpp
    test.cpp: In function `int main()':
    test.cpp:7: warning: division by zero in `1 / 0'
     
    NFish, Nov 9, 2003
    #9
    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. Bryan Bullard
    Replies:
    17
    Views:
    590
    Donovan Rebbechi
    Jan 11, 2004
  2. flamesrock
    Replies:
    8
    Views:
    548
    Hendrik van Rooyen
    Nov 24, 2006
  3. Nagaraj
    Replies:
    1
    Views:
    913
    Lionel B
    Mar 1, 2007
  4. Carter
    Replies:
    2
    Views:
    527
    Carter
    Mar 4, 2009
  5. Stargazer

    Re: Forcing compile-time error

    Stargazer, Jun 11, 2010, in forum: C Programming
    Replies:
    3
    Views:
    381
    Eric Sosman
    Jun 12, 2010
Loading...

Share This Page