473,386 Members | 1,708 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

forcing a compile time error

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
Jul 19 '05 #1
8 5961
> 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
Jul 19 '05 #2
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.

Jul 19 '05 #3
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);

Jul 19 '05 #4

"Jack Adam" <ja*******@gmx.net> wrote in message
news:bo**********@newsg1.svr.pol.co.uk...
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.
Jul 19 '05 #5
On Sat, 8 Nov 2003 17:53:32 +0100, "Severin Ecker" <se****@gmx.at>
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
Jul 19 '05 #6
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.

Jul 19 '05 #7
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 );
}

Jul 19 '05 #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?

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'

Jul 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Bryan Bullard | last post by:
hi, is there a way to force the user of an object to catch exceptions of specific type? for instance a compile error is issued if exception 'ExeceptionXXX' thrown by method...
5
by: Carmine Cairo | last post by:
Hi, I'm working on a project and today I've note a little problem during the compile fase. Here a little piece of code: // 1st version welldone = 0; size = p->getSize(); backbone = new...
2
by: Glen | last post by:
I'm working on a custom assembly and I'm trying to figure out the best approach to handling known constraints within the assembly, once compiled, to alert the developer at compile time of a...
13
by: orekin | last post by:
Hi There I have been programming C# for a couple of months and am trying to master Threading. I understand that ThreadPool uses background threads (see code example in MSDN page titled...
0
by: ufnuceda | last post by:
Hello everyone, I was wondering if any of you have some experience with the boost library. I am having trouble compiling code with it. Since boost is being used a lot these days I thought some...
1
by: electrixnow | last post by:
Help!, I need to compile this code with static libs so it run on another XP machine that does'nt have MS Studio installed. When I compile now I get an ERROR: 1>------ Rebuild All started:...
4
by: Dan Krantz | last post by:
I have the following template to ensure that a given number (val) falls into a range (between vmin & vmax): template<typename T> T ForceNumericRange( const T& val, const T& vmin, const T& vmax)...
9
by: ThunderMusic | last post by:
Hi, I'd like to create a compile time error in my class... maybe there's a way already built in in the framework so I can achieve what I want... I have 2 constructors in my class. One of them...
5
by: David Thielen | last post by:
Hi; We keep having to restart IIS after ASP.NET kills it. Below is what we have in the event log. Any idea what the problem is? thanks - dave Event code: 3003 Event message: A validation...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.