473,779 Members | 2,001 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5980
> 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.p ol.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.a t>
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.l earn.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_i f_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
3146
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 'ThrowsExceptionXXX' is not caught. thanks,
5
3336
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 rightType;
2
4646
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 potential issue. For example, in the assembly I would like to add a constraint that states a particular property member of the class can not be equal to one other property. In standard coding I can throw an exception during run-time, but I would rather...
13
2522
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 'ThreadPool Class ') .... however I would like to ensure that all my ThreadPool threads have completed before I exit my Main function.
0
2516
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 of you might have an answer. I would greatly appreciate help with this, as I tried to search for an answer for quite some time in vain. I am getting error messages when I try to compile as soon as I put an include to the boost library in the...
1
5396
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: Project: drawing_control, Configuration: Release Win32 ------ 1>Deleting intermediate and output files for project 'drawing_control', configuration 'Release|Win32'
4
3105
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) { T retVal = val; if ( retVal < vmin ) retVal = vmin;
9
3519
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 has mandatory parameters, I mean, they should not be null nor empty (for strings). So I'd make the validation in the constructor and generate a compile-time error if the validation does not match... Is there a way to achieve this or to specify...
5
3238
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 error has occurred. Event time: 6/23/2008 9:07:24 AM
0
9632
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9471
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10071
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9925
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8958
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7478
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5372
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5501
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4036
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.