为了尝试一下,我最近决定尝试用 C++ 实现一个模板化的复数类。作为引用,我使用了 C++ 11 标准库实现,我发现该实现与我的有所不同。它指的是他们如何在他们的类中重载 += 运算符。
在我的中,我基本上只有一个 += 方法,它能够同时处理 Complex<T> += Complex<U>
和 Complex<T> += whatever other type that implicitly converts to T and thus Complex<T>
.为了澄清这里是类声明:
template <typename T>
class Complex {
public:
constexpr Complex (T=T(), T=T()); // added for clarity
template <typename U>
constexpr Complex (const Complex <U>&);
template <typename U>
constexpr Complex (Complex <U>&&);
template <typename U>
Complex <T>& operator= (const Complex <U>&);
template <typename U>
Complex <T>& operator= (Complex <U>&&);
Complex& operator+= (const Complex<T>&);
Complex& operator-= (const Complex<T>&);
Complex& operator*= (const Complex<T>&);
Complex& operator/= (const Complex<T>&);
Complex& operator++ (void);
Complex operator++ (int);
Complex& operator-- (void);
Complex operator-- (int);
std::string to_string (void) const;
constexpr T real (void);
constexpr T img (void);
void real (const T&);
void img (const T&);
private:
T _m_real, _m_img;
template <typename U>
friend class Complex;
};
但是在标准库实现中,他们对 operator+=
使用了 2 个重载,一个需要 Complex<U>
另一个需要 T
.据我测试,这两种实现似乎产生了相同的行为:在任何两个复杂类型之间或一个复杂类型与另一种隐式转换为复杂内部类型的类型之间的加法。
所以,我的问题是:是否有任何理由需要单独的 operator+=(T)
除了优化一个临时的复合体以及为什么使用 nestde template <typename U>
如果所有其他复杂类型都隐式转换为 Complex?
最佳答案
struct Foo {
operator int()const {
return 7;
}
};`
递增 Foo
会遇到问题,因为只会调用一个用户定义的转换。
举个例子:
Complex<double> d;
Foo f;
d += f; // fails to compile with your version, compiles with C++ standard one
还有一个区别。如果我们有一个类 Bar
有一个 operator Complex<double>() const
,它将无法与 C++ 标准版本一起使用。
对于你的版本,它可以工作当且仅当 T
正是double
.
简而言之,您的参数可以隐式转换为特定类型或从特定类型隐式转换与采用该类型的参数不同。只能尝试一种用户定义的转换,所以如果您采用 int
可以转换为 int
的类型是可以接受的,如果您采用可以由 int
制成的类型,并非所有类型都可以转换为 int
是可以接受的。
非用户定义的转换不受相同方式的限制。
关于c++ - 模板重载解析和隐式转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19641016/