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

why standard convesion is picked instead of constructor conversion

tom
I have a code segment list below, for the function call "calc()" in
the main function, a standard conversion from "double"->"int" is made
while "double"->"LongDouble" is also viable, does anyone know why
standard conversion is picked as the best match by the compilier?

#include <iostream>

class LongDouble
{
public:
operator double() const
{
return 2.0;
}
};

void calc(int a)
{
std::cout<<"void calc(int a) called"<<std::endl;
}
void calc(LongDouble a)
{
std::cout<<"void calc(LongDouble a)"<<std::endl;
}

int main(int argc, char *argv[])
{
double dval = 0.0;
calc(dval);
return 0;
}

Sep 1 '07 #1
4 1432
On 2007-09-01 20:58, tom wrote:
I have a code segment list below, for the function call "calc()" in
the main function, a standard conversion from "double"->"int" is made
while "double"->"LongDouble" is also viable, does anyone know why
standard conversion is picked as the best match by the compilier?
You've got it backwards, a conversion from LongDouble to duble is
possible but not the other way around. To make that conversion possible
you have to add a constructor taking a double to LongDouble.

--
Erik Wikström
Sep 1 '07 #2
tom
On Sep 2, 7:28 am, Erik Wikström <Erik-wikst...@telia.comwrote:
On 2007-09-01 20:58, tom wrote:
I have a code segment list below, for the function call "calc()" in
the main function, a standard conversion from "double"->"int" is made
while "double"->"LongDouble" is also viable, does anyone know why
standard conversion is picked as the best match by the compilier?

You've got it backwards, a conversion from LongDouble to duble is
possible but not the other way around. To make that conversion possible
you have to add a constructor taking a double to LongDouble.

--
Erik Wikström
Sorry, my mistake (in the double->LongDouble conversion, is should be
a constructor), my question is actually for the code below:

#include <iostream>
class LongDouble
{
public:
// double -LongDouble conversion
LongDouble(double dval)
{
}
};

void calc(int a)
{
std::cout<<"void calc(int a) called"<<std::endl;
}

void calc(LongDouble a)
{
std::cout<<"void calc(LongDouble a)"<<std::endl;
}

int main(int argc, char *argv[])
{
double dval = 0.0;
calc(dval);
return 0;
}

Sep 2 '07 #3
On Sep 1, 11:52 pm, tom <pxk...@gmail.comwrote:
Sorry, my mistake (in the double->LongDouble conversion, is should be
a constructor), my question is actually for the code below:

#include <iostream>
class LongDouble
{
public:
// double -LongDouble conversion
LongDouble(double dval)
{
}

};

void calc(int a)
{
std::cout<<"void calc(int a) called"<<std::endl;

}

void calc(LongDouble a)
{
std::cout<<"void calc(LongDouble a)"<<std::endl;

}

int main(int argc, char *argv[])
{
double dval = 0.0;
calc(dval);
return 0;

}
In this case, converting the double argument to an int parameter is
considered a "better" match than converting the double argument to a
LongDouble parameter. According to the C++ Standard, standard
conversions (like double-to-int) have a higher rank than user-defined
conversions (such as double-to-LongDouble).

This point can be tested by adding another calc() overload:

void calc( long double ld) // Error: ambiguous
{
}

With this addition, there are now two possible standard conversions
(int-to-double and double-to-long double), so there is no clear
favorite anymore and the call to calc() is now ambiguous.

The best approach is probably not to mix user-defined and built-in
types in this manner. I would either have the program use the "long
double" built-in type instead of a LongDouble user-defined type - or -
I would keep the LongDouble type and define an Integer type to go
along with it.

Greg
Sep 3 '07 #4
tom
On Sep 3, 4:58 pm, Greg Herlihy <gre...@pacbell.netwrote:
On Sep 1, 11:52 pm, tom <pxk...@gmail.comwrote:
Sorry, my mistake (in the double->LongDouble conversion, is should be
a constructor), my question is actually for the code below:
#include <iostream>
class LongDouble
{
public:
// double -LongDouble conversion
LongDouble(double dval)
{
}
};
void calc(int a)
{
std::cout<<"void calc(int a) called"<<std::endl;
}
void calc(LongDouble a)
{
std::cout<<"void calc(LongDouble a)"<<std::endl;
}
int main(int argc, char *argv[])
{
double dval = 0.0;
calc(dval);
return 0;
}

In this case, converting the double argument to an int parameter is
considered a "better" match than converting the double argument to a
LongDouble parameter. According to the C++ Standard, standard
conversions (like double-to-int) have a higher rank than user-defined
conversions (such as double-to-LongDouble).

This point can be tested by adding another calc() overload:

void calc( long double ld) // Error: ambiguous
{
}

With this addition, there are now two possible standard conversions
(int-to-double and double-to-long double), so there is no clear
favorite anymore and the call to calc() is now ambiguous.

The best approach is probably not to mix user-defined and built-in
types in this manner. I would either have the program use the "long
double" built-in type instead of a LongDouble user-defined type - or -
I would keep the LongDouble type and define an Integer type to go
along with it.

Greg
Thanks for the reply and good advice.

Sep 5 '07 #5

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

Similar topics

18
by: Marcin Kalicinski | last post by:
Hi, Why is there void* conversion to check for std::istream failure bit? Why not a conversion to bool? When I try to return an istream from a function that actually returns bool, I get the...
27
by: djake | last post by:
In the stroustrup C++ programming language (third edition) i can find example like this: string s1= "Hello"; So I imagine string is a standard class. Furthermore the class in the example is...
43
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own...
5
by: Ross A. Finlayson | last post by:
Hi, I'm scratching together an Access database. The development box is Office 95, the deployment box Office 2003. So anyways I am griping about forms and global variables. Say for example...
19
by: junky_fellow | last post by:
How the unsigned to signed integet conversion is done ? For eg: unsigned int ui = 100; int si = ui; What will be the value if "si" is printed ? How this conversion is done ? Thanx for any...
19
by: Martin Oddman | last post by:
Hi, I have a compiling problem. Please take a look at the code below. I have an application that is built upon three tiers: one data tier (Foo.DataManager), one business tier (Foo.Kernel) and...
32
by: toolmaster | last post by:
Since many of the modern computer languages have built-in namespace features, I can't understand why not add this feature into standard C. I've heard many people complain of the lacking of...
3
by: Jess | last post by:
Hello, I can perform implicit conversion through constructor, like class A{ public: A(int x):a(x){}; int a; };
16
by: Andrea Gavana | last post by:
Hi Diez & All, Do you mind explaining "why" you find it *buttugly*? I am asking just out of curiosity, obviously. I am so biased towards wxPython that I won't make any comment on this thread...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
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...
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
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
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.