|
|
about conversion operator overload
Hello, I meet a question about conversion operator overload,please
help me, thanks. Here is the program:
struct return_overload
{
unsigned int data;
return_overload(unsigned int init_data):data(init_data){};
template<typename T>
operator T() const {std::cout << "operator() T"<<std::endl;
return T(double(data)/2);}
operator unsigned int () const {std::cout << "operator
unsigned int()" << std::endl;return data >> 1;}
};
return_overload binary(unsigned int data)
{
return return_overload(data);
}
int main(int argc, char* argv[])
{
double a;
unsigned int b;
a = binary(123);
b = binary(123);
std::cin.get();
return 0;
}
the output under g++, CC and VC.net are:
operator unsigned int
operator unsigned int
I want to know why the compiler don't pick the template version.
thanks.
andyzhang from china.
11/2/2003
______________________________________
===================================================================
����2003��Ʒȫ�����У���Ʒ�ʣ���ѡ�� (http://ad4.sina.com.cn/shc/zhuiyu_hprefresh.html)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
zhstrongman (1)
|
11/2/2003 12:12:06 PM |
|
2 Nov 2003 07:12:06 -0500 , zhstrongman <zhstrongman@sina.com> :
>Hello, I meet a question about conversion operator overload,please
>help me, thanks. Here is the program:
>
>struct return_overload
>{
> unsigned int data;
> return_overload(unsigned int init_data):data(init_data){};
>
> template<typename T>
> operator T() const {std::cout << "operator() T"<<std::endl;
> return T(double(data)/2);}
>
> operator unsigned int () const {std::cout << "operator
> unsigned int()" << std::endl;return data >> 1;}
>};
>return_overload binary(unsigned int data)
>{
> return return_overload(data);
>}
>
>int main(int argc, char* argv[])
>{
>double a;
>unsigned int b;
>a = binary(123);
>b = binary(123);
>std::cin.get();
>return 0;
>}
>
>the output under g++, CC and VC.net are:
>operator unsigned int
>operator unsigned int
>
>I want to know why the compiler don't pick the template version.
>thanks.
this is not compiler's bug.
the compiler will not maching a template conversion operator T() with
this program, template will not conclude which conversion what you
needed with your return value.
you can wrote conversion explicit, for ex:
return_overload ra(10);
ra.template operator double<double>();
i am chinese programmer too, visit http://coneos.126.com :-)
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
DarkSpy
|
11/4/2003 1:15:42 AM
|
|
zhstrongman wrote:
> Hello, I meet a question about conversion operator overload,please
> help me, thanks. Here is the program:
>
> struct return_overload
> {
> unsigned int data;
> return_overload(unsigned int init_data):data(init_data){};
>
> template<typename T>
> operator T() const {std::cout << "operator() T"<<std::endl;
> return T(double(data)/2);}
>
> operator unsigned int () const {std::cout << "operator
> unsigned int()" << std::endl;return data >> 1;}
> };
> return_overload binary(unsigned int data)
> {
> return return_overload(data);
> }
>
> int main(int argc, char* argv[])
> {
> double a;
> unsigned int b;
> a = binary(123);
> b = binary(123);
> std::cin.get();
> return 0;
> }
>
> the output under g++, CC and VC.net are:
> operator unsigned int
> operator unsigned int
>
> I want to know why the compiler don't pick the template version.
It works the way you expect in VC++ 7.1 (Visual Studio .NET 2003).
I think the other compilers you tried are incorrect.
Making operator unsigned int an out-of-line specialisation of
template operator T works in both g++ 3.0 and VC++ 7.1, but not
in earlier versions of VC++:
template<>
return_overload::operator unsigned int () const {
std::cout << "operator unsigned int()" << std::endl;
return data >> 1;
}
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
0
|
|
|
|
Reply
|
Ben
|
11/4/2003 1:16:46 AM
|
|
|
2 Replies
148 Views
(page loaded in 0.521 seconds)
|
|
|
|
|
|
|
|
|