473,809 Members | 2,787 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Avoid implicit cast from enum to double

Flo

I have the problem, that the following Code produces neither warning
nor error at compile time, at least not with the Microsoft Visual C++
6.0 compiler (warning level 4)

enum E { eOne, eTwo };
struct S {
void fooDouble( double ) {};
void fooEnum( E ) {};
};
int main() {
S s;
s.fooDouble( eOne ); // neither warning nor error
return 0;
}

As you can see, accidentally an enum is passed to the method expecting
an double. In my real program, this leads to an error at run-time,
since fooDouble only accepts quit small values, i.e. values smaller
than 1.0. Has anybody an idea what I can do, maybe changes in the
methodnames/parameterlist, to provoke a warning at compile time so one
can not accidentally pass an enum to the method expecting a double?

Flo

Flo

Jul 18 '06 #1
2 4224
Flo wrote:
I have the problem, that the following Code produces neither warning
nor error at compile time, at least not with the Microsoft Visual C++
6.0 compiler (warning level 4)

enum E { eOne, eTwo };
struct S {
void fooDouble( double ) {};
void fooEnum( E ) {};
Please drop the habit of appending a semicolon after a function body.
It just looks sloppy.
};
int main() {
S s;
s.fooDouble( eOne ); // neither warning nor error
return 0;
}

As you can see, accidentally an enum is passed to the method expecting
an double. In my real program, this leads to an error at run-time,
since fooDouble only accepts quit small values, i.e. values smaller
than 1.0. Has anybody an idea what I can do, maybe changes in the
methodnames/parameterlist, to provoke a warning at compile time so one
can not accidentally pass an enum to the method expecting a double?
Wrap your enum in a struct and pass the struct. Inside just access the
wrapped value.

Check the value (or assert if you can debug all cases of using your
function) inside your 'fooDouble' function.

Be careful and just don't write code that passes the enum to 'fooDouble'.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 18 '06 #2
Flo wrote:
I have the problem, that the following Code produces neither warning
nor error at compile time, at least not with the Microsoft Visual C++
6.0 compiler (warning level 4)

enum E { eOne, eTwo };
struct S {
void fooDouble( double ) {};
void fooEnum( E ) {};
};
int main() {
S s;
s.fooDouble( eOne ); // neither warning nor error
return 0;
}
Well, it can be converted implicitly.
As you can see, accidentally an enum is passed to the method expecting
an double. In my real program, this leads to an error at run-time,
since fooDouble only accepts quit small values, i.e. values smaller
than 1.0. Has anybody an idea what I can do, maybe changes in the
methodnames/parameterlist, to provoke a warning at compile time so one
can not accidentally pass an enum to the method expecting a double?
Remove the type prefix from the function name and call your functions both
just foo. Then the compiler will automatically choose the right function.

Jul 18 '06 #3

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

Similar topics

15
2303
by: buda | last post by:
Let me see if I got this :) 1. I know the rules for type conversions in arithmetic expressions 2. I know that an implicit type conversion is done at assignment, so float x = 1.23; int t = (int) x; is equivalent to int t = x; (could the latter produce a warning on some complier?) 3. I know that implicit conversions take place with function arguments, but am a bit shaky here. I suppose that passing a char to a function accepting
1
18267
by: Martin Bischoff | last post by:
Hi, is it possible to define an implicit user-defined type conversion operator for enums? I'd like to convert my enum to an int without an explicit cast, e.g. something like this: ------ public enum MyEnum : int
11
7628
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type Test. I expected the same error from the following line, but it compiles fine. The double is silently truncated to an int and then fed in to the implicit conversion operator. Why does this happen? Is there any way that I can keep the implicit...
4
1798
by: FB's .NET Dev PC | last post by:
Interesting note, the code below as is will attempt to cast what is clearly indicated as a string into a double. This is becuase the use of + as a concatenation operator. The error message generated is included with the code. Use & instead and it works. Dim pstrSend As String = "" 'ActionCode 1 and 2 are type BYTE, Msg is a byVal string parameter
1
3306
by: Joe HM | last post by:
Hello - I have two Enums for which I would like to define type conversions ... Public Enum eA A2 = 0 A2 = 1 End Enum Public Enum eB B1 = 2
17
2522
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
1
3194
by: not_a_commie | last post by:
I have an Angle class that I store angles in. It's basically just a bunch of fancy functions for manipulating a double. It has implicit casts for converting to/from double. Due to the project structure and where it stores the preferred SI units for display, I wanted to have the TypeConverter in a different project that has no reference to the angle class. However, returning a double from the ConvertFrom function throws an error that it...
1
9538
by: perroe | last post by:
Hi I have a array of complex numbers that are stored in a simple double array. This is done since the array is part of an wrapper for an external C library, and the imaginary part of the first element, and the last element are known to be 0. I've implemented a -operator that returns a ComplexReference object that basically maps a complex<doubleinto the storage used in the array. What I would like to do is using the ComplexReference...
17
2530
by: arnuld | last post by:
I have checked the FAQ: http://c-faq.com/malloc/mallocnocast.html FAQ discusses a special case when programmer has forgotten to do #include <stdlib.h>. I am including this header and I am not doing any explicit cast: #include <stdlib.h>
0
9721
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
9601
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,...
0
10637
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9199
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...
0
5550
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
5687
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4332
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
2
3861
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3014
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.