由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 请问一个implicit conversion的问题(C++)
相关主题
c++ operator overloading questiona simple question about constructor
怎么搞的?发现一个有趣的现象。
[合集] 又学了一招one question about initializaiton list
问个overloading new operator的问题question regarding const function
小问题感觉学scala不学haskell是不行的
[合集] question on item15 in Effective C++请教几个C++问题
An object of A automatically converted to an object of B.C++ implicit typename的问题
a string define question (c++)【请教】mpicc 和 mpiCC编译问题
相关话题的讨论汇总
话题: class话题: conversion话题: c++话题: implicit话题: defined
进入Programming版参与讨论
1 (共1页)
h**o
发帖数: 347
1
原例抄自于Bjarne的书p 275,让自己定义的类参于已存在的类的运算
代码如H下
h**o
发帖数: 347
2
re啊,问题是长了一点。。。

【在 h**o 的大作中提到】
: 原例抄自于Bjarne的书p 275,让自己定义的类参于已存在的类的运算
: 代码如H下

g***e
发帖数: 577
3
I read it, but I dont know the answer.:)

【在 h**o 的大作中提到】
: re啊,问题是长了一点。。。
X****r
发帖数: 3557
4
In case of a function template, template argument deduction is performed
before the compiler tries to resolve the overloading [13.3.1 paragraph 7].
In other words, the compiler sees "a*b", then it looks for all overloaded "
operator*". You have "template operator *(const A&, const A&)
;" defined up there, so it tries to deduct the typename T. Note that the
compiler doesn't look into anything else in this step, including your user-
defined conversion function. Obviously it cannot f
h**o
发帖数: 347
5
thx

【在 g***e 的大作中提到】
: I read it, but I dont know the answer.:)
h**o
发帖数: 347
6
谢谢,我明白这层意思了
之所以我想用implicit conversion,就是因为class A已经有很多运算了
一个一个再重写既麻烦,我又不知道是不是多余。。。

&)
so
all

【在 X****r 的大作中提到】
: In case of a function template, template argument deduction is performed
: before the compiler tries to resolve the overloading [13.3.1 paragraph 7].
: In other words, the compiler sees "a*b", then it looks for all overloaded "
: operator*". You have "template operator *(const A&, const A&)
: ;" defined up there, so it tries to deduct the typename T. Note that the
: compiler doesn't look into anything else in this step, including your user-
: defined conversion function. Obviously it cannot f

h**o
发帖数: 347
7
I get most of your points. But could you belabor a little on your last
sentence please?
The problem I have is that class A is actually built-in in STL and it has
a lot of non-member binary operations. Class B is defined by me. It is a
variant of class A. What I am looking for is a way to implicitly convert
B to A when invoking any function defined for A.
I don't know if this kind of shortcut exits. It is impossible to define a
base class and it looks verbose to define all combinations of A & B f

【在 X****r 的大作中提到】
: In case of a function template, template argument deduction is performed
: before the compiler tries to resolve the overloading [13.3.1 paragraph 7].
: In other words, the compiler sees "a*b", then it looks for all overloaded "
: operator*". You have "template operator *(const A&, const A&)
: ;" defined up there, so it tries to deduct the typename T. Note that the
: compiler doesn't look into anything else in this step, including your user-
: defined conversion function. Obviously it cannot f

1 (共1页)
进入Programming版参与讨论
相关主题
【请教】mpicc 和 mpiCC编译问题小问题
请教C++问题[合集] question on item15 in Effective C++
C++指针问题 int (*) [10]An object of A automatically converted to an object of B.
could anybody please tell me what " while(cin) {...}" means?a string define question (c++)
c++ operator overloading questiona simple question about constructor
怎么搞的?发现一个有趣的现象。
[合集] 又学了一招one question about initializaiton list
问个overloading new operator的问题question regarding const function
相关话题的讨论汇总
话题: class话题: conversion话题: c++话题: implicit话题: defined