boost::运算符混合算术
基于此处的示例 http://www.boost。 org/doc/libs/release/libs/utility/operators.htm#example,我已经实现了以下 boost::numeric::ublas::vector
的派生类:
namespace Chebyshev
{
template<typename T>
class function_data : public boost::numeric::ublas::vector<T>,
boost::addable<function_data<T> >,
boost::subtractable<function_data<T> >,
boost::multipliable2<function_data<T>, T>,
boost::dividable2<function_data<T>, T>
{
public:
char dataflag;
function_data() : boost::numeric::ublas::vector<T>() {dataflag=0;} ///< The default empty constructor
function_data(const boost::numeric::ublas::vector<T>& vec) : boost::numeric::ublas::vector<T>(vec) {dataflag=0;} ///< The copy constructor without a flag.
function_data(const boost::numeric::ublas::vector<T>& vec, char flag) : boost::numeric::ublas::vector<T>(vec), dataflag(flag) {} ///< The copy constructor with a flag.
~function_data() {} ///< The destructor.
function_data<T>& operator= (const boost::numeric::ublas::vector<T>& in) {boost::numeric::ublas::vector<T>::operator=(in); return *this;} ///< The assignment operator from a boost::numeric::ublas::vector<T>.
function_data<T>& operator= (const function_data<T>& in) {boost::numeric::ublas::vector<T>::operator=(in); dataflag=in.dataflag; return *this;} ///< The assignment operator.
function_data<T>& operator+= (const function_data<T>& in) {this->boost::numeric::ublas::vector<T>::operator+=(in); this->dataflag=this->dataflag; return *this;}
function_data<T>& operator-= (const function_data<T>& in) {this->boost::numeric::ublas::vector<T>::operator-=(in); this->dataflag=this->dataflag; return *this;}
function_data<T>& operator*= (T in) {this->boost::numeric::ublas::vector<T>::operator*=(in); this->dataflag=this->dataflag; return *this;}
function_data<T>& operator/= (T in) {this->boost::numeric::ublas::vector<T>::operator/=(in); this->dataflag=this->dataflag; return *this;}
friend std::ostream& operator<< (std::ostream& os, const function_data<T>& fd) {os << "[type " << fd.dataflag << "] " << static_cast<boost::numeric::ublas::vector<T> >(fd); return os;} ///< The << operator.
};
}
但是,编译以下代码片段代码
int main( int argc, char ** argv)
{
Chebyshev::function_data<std::complex<double> > u;
/* some stuff putting values in u */
std::cout << u*2 << std::endl;
return 0;
}
给出了“ISO C++ 说这些是不明确的,即使第一个的最差转换比第二个的最差转换要好”警告,并继续给出 ublas vector_expression 版本(带有 u
转换为某种向量表达式)和我的版本(2
转换为 const std::complex
)。
我希望能够在我的类中使用混合算术,如上面的代码片段所示,但 boost::operators 网站上的解释对我来说并不清楚。我必须在班级中添加或更改哪些内容才能实现此目的?
此外,在该示例中,继承列表将每个类都放在前一个类的最后一个 >
内。无论我是这样写还是按照上面的方式编写,我都没有看到编译器的输出有任何差异。哪种写法才是正确的呢?
最好的问候,布雷特。
based on the example here http://www.boost.org/doc/libs/release/libs/utility/operators.htm#example, I have implemented the following derived class of boost::numeric::ublas::vector
:
namespace Chebyshev
{
template<typename T>
class function_data : public boost::numeric::ublas::vector<T>,
boost::addable<function_data<T> >,
boost::subtractable<function_data<T> >,
boost::multipliable2<function_data<T>, T>,
boost::dividable2<function_data<T>, T>
{
public:
char dataflag;
function_data() : boost::numeric::ublas::vector<T>() {dataflag=0;} ///< The default empty constructor
function_data(const boost::numeric::ublas::vector<T>& vec) : boost::numeric::ublas::vector<T>(vec) {dataflag=0;} ///< The copy constructor without a flag.
function_data(const boost::numeric::ublas::vector<T>& vec, char flag) : boost::numeric::ublas::vector<T>(vec), dataflag(flag) {} ///< The copy constructor with a flag.
~function_data() {} ///< The destructor.
function_data<T>& operator= (const boost::numeric::ublas::vector<T>& in) {boost::numeric::ublas::vector<T>::operator=(in); return *this;} ///< The assignment operator from a boost::numeric::ublas::vector<T>.
function_data<T>& operator= (const function_data<T>& in) {boost::numeric::ublas::vector<T>::operator=(in); dataflag=in.dataflag; return *this;} ///< The assignment operator.
function_data<T>& operator+= (const function_data<T>& in) {this->boost::numeric::ublas::vector<T>::operator+=(in); this->dataflag=this->dataflag; return *this;}
function_data<T>& operator-= (const function_data<T>& in) {this->boost::numeric::ublas::vector<T>::operator-=(in); this->dataflag=this->dataflag; return *this;}
function_data<T>& operator*= (T in) {this->boost::numeric::ublas::vector<T>::operator*=(in); this->dataflag=this->dataflag; return *this;}
function_data<T>& operator/= (T in) {this->boost::numeric::ublas::vector<T>::operator/=(in); this->dataflag=this->dataflag; return *this;}
friend std::ostream& operator<< (std::ostream& os, const function_data<T>& fd) {os << "[type " << fd.dataflag << "] " << static_cast<boost::numeric::ublas::vector<T> >(fd); return os;} ///< The << operator.
};
}
However, compiling the following snippet of code
int main( int argc, char ** argv)
{
Chebyshev::function_data<std::complex<double> > u;
/* some stuff putting values in u */
std::cout << u*2 << std::endl;
return 0;
}
gives a "ISO C++ says that these are ambiguous, even though the worst conversion for the first is better than the worst conversion for the second" warning and proceeds to give the ublas vector_expression version (with u
cast as some kind of vector_expression) and my version (with 2
cast as a const std::complex<double>&
).
I would like to be able to use mixed arithmetic in my class as in the above snippet of code, but the explanation on the boost::operators website isn't clear to me. What do I have to add or change in my class to allow this?
Also, in the example, the inheritance list has each class inside the last >
of the previous class. I don't see any difference in the output of the compiler whether I write it that way or the way I have above. Which is the proper way to write it?
Best regards, Brett.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Boost uBLAS 具有向量算术所需的所有运算符。下面是一个示例程序来强调它:
这是我使用 g++ v4.1.2 编译和运行时得到的输出:
Boost uBLAS has all the operators that you need to vector arithmetic. Here is a sample program to highlight it:
Here is the output I got when I compiled and ran using g++ v4.1.2: