473,396 Members | 1,780 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,396 software developers and data experts.

Selecting implicit type conversions?

I'm developing an API that I want be as user friendly as possible. My
problem is that there are functions that can take many optional
parameters (of various type) and I want to be able to specify them in
an arbitrary order (forget about default values). I think I figured
out a simple way of doing this but I still have one little annoyance
(actually it's more a curiosity than an annoyance).

Here's a simplified example. In the real thing I'm using va_arg for
the variable length argument list in the function f (hence the union
instead of a template).

#include <iostream>
using namespace std;

enum{PARAMETER1, PARAMETER2};

class A{
public:

union Value{
int int_value;
float float_value;
};

int ID;
Value value;

A(int i){ID = i;}

// PARAMETER1 is of type int
A operator = (int i){
if(ID == PARAMETER1) // sanity check
value.int_value = i;
return *this;
}

// PARAMETER2 is of type float
A operator = (float f){
if(ID == PARAMETER2) // sanity check
value.float_value = f;
return *this;
}
};

void f(A a){
if(a.ID == PARAMETER1)
cout<<a.value.int_value<<endl;
if(a.ID == PARAMETER2)
cout<<a.value.float_value<<endl;
}

A parameter1(PARAMETER1);
A parameter2(PARAMETER2);

int main(){

f(parameter1=1);
f(parameter2=2.52); // LINE 1
f(parameter2=2.52f); // LINE 2

return 1;
}

LINE 1 doesn't compile. It tries to figure out which '=' to use
test.cc: In function `int main()':
test.cc:47: ambiguous overload for `A& = double' operator
test.cc:21: candidates are: A A::operator=(int)
test.cc:26: A A::operator=(float)
test.cc:7: A& A::operator=(const A&)

LINE 2 works fine. Howerver I wonder if it is possible to achieve the
same result without the constant suffixe (or an explicit conversion to
float).

Can I have the compiler to figure that it's better to convert a double
to a float than an int?

Any thoughts?

Thanks.
Jul 19 '05 #1
1 2058

"Simon Leonard" <sl******@cs.ualberta.ca> wrote in message
news:4b*************************@posting.google.co m...
I'm developing an API that I want be as user friendly as possible. My You propably may want to consider voice or gesture interface indeed :).

[...]
Can I have the compiler to figure that it's better to convert a double
to a float than an int?

I am afraid not, you cannot. You are to add A::operator=(double) operator or
use explicit conversion.

--
Michael Kochetkov.
Jul 19 '05 #2

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

Similar topics

3
by: Reneé | last post by:
I wanted to know the order of implicit conversions and which sort of values allow them. From searching around in books and the archive of this mailing list, it seems to be that only numbers are...
9
by: Simon | last post by:
Hi All, Is it possible to disallow implicit casting for an operand of a function written in C? i.e. void foo(int a) {..} short b; foo(b) // error without explicit cast
15
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)...
11
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...
9
by: Girish | last post by:
Im trying to understand implicit type conversions from object -> string and vice versa. I have two classes, one Driver and one called StringWrapper. These are just test classes that try and...
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
36
by: Chad Z. Hower aka Kudzu | last post by:
I have an implicit conversion set up in an assembly from a Stream to something else. In C#, it works. In VB it does not. Does VB support implicit conversions? And if so any idea why it would work...
17
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
82
by: robert bristow-johnson | last post by:
here is a post i put out (using Google Groups) that got dropped by google: i am using gcc as so: $ gcc -v Using built-in specs. Target: i386-redhat-linux Configured with: ../configure...
8
by: jonpb | last post by:
Hi, Is it possible to define a implicit operator from base to derived types in C#? I have a Point class and would like to derive a Vector class from it and add a couple new vector related...
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
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
0
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...
0
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...
0
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...
0
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,...

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.