我在编译器相关问题中遇到以下代码(存储在crtp.cc中):
#include <vector>
#include <cassert>
#include <iostream>
template < class Derived >
class AlgebraicVectorExpression {
public:
typedef std::vector<double>::size_type SizeType;
typedef std::vector<double>::value_type ValueType;
typedef std::vector<double>::reference ReferenceType;
SizeType size() const {
return static_cast<const Derived&>(*this).size();
}
ValueType operator[](SizeType ii) const {
return static_cast<const Derived&>(*this)[ii];
}
operator Derived&() {
return static_cast<Derived&>(*this);
}
operator const Derived&() const {
return static_cast< const Derived& >(*this);
}
};
template< class T1, class T2>
class AlgebraicVectorSum : public AlgebraicVectorExpression< AlgebraicVectorSum<T1,T2> > {
const T1 & a_;
const T2 & b_;
typedef typename AlgebraicVectorExpression< AlgebraicVectorSum<T1,T2> >::SizeType SizeType;
typedef typename AlgebraicVectorExpression< AlgebraicVectorSum<T1,T2> >::ValueType ValueType;
public:
AlgebraicVectorSum(const AlgebraicVectorExpression<T1>& a, const AlgebraicVectorExpression<T1>& b) :
a_(a), b_(b) {
assert(a_.size() == b_.size());
}
SizeType size() const {
return a_.size();
}
ValueType operator[](SizeType ii) const {
return (a_[ii] + b_[ii]);
}
};
template< class T1, class T2>
const AlgebraicVectorSum<T1,T2>
operator+(const AlgebraicVectorExpression<T1>& a, const AlgebraicVectorExpression<T2>& b) {
return AlgebraicVectorSum<T1,T2>(a,b);
}
class AlgebraicVector : public AlgebraicVectorExpression<AlgebraicVector>{
std::vector<double> data_;
public:
SizeType size() const {
return data_.size();
}
ValueType operator[](SizeType ii) const {
return data_[ii];
}
ValueType& operator[](SizeType ii) {
return data_[ii];
}
AlgebraicVector(SizeType n) : data_(n,0.0) {
};
template< class T>
AlgebraicVector(const AlgebraicVectorExpression<T>& vec) {
const T& v = vec;
data_.resize(v.size());
for( SizeType idx = 0; idx != v.size(); ++idx) {
data_[idx] = v[idx];
}
}
};
int main() {
AlgebraicVector x(10);
AlgebraicVector y(10);
for (int ii = 0; ii != 10; ++ii)
x[ii] = y[ii] = ii;
AlgebraicVector z(10);
z = x + y;
for(int ii = 0; ii != 10; ++ii)
std::cout << z[ii] << std::endl;
return 0;
}
事实上当我用:
编译它时$ g++ --version
g++ (Ubuntu 4.4.3-4ubuntu5) 4.4.3
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ g++ -O0 -g crtp.cc
我获得:
$ ./a.out
0
2
4
6
8
10
12
14
16
18
这是预期的行为。当我使用icpc时:
$ icpc --version
icpc (ICC) 12.1.0 20110811
Copyright (C) 1985-2011 Intel Corporation. All rights reserved.
$ icpc -g -O0 crtp.cc
我取而代之的是Segmentation fault
。运行
valgrind --tool=memcheck ./a.out
指向来源的第29行
AlgebraicVectorExpression<AlgebraicVector>::operator AlgebraicVector const&() const (crtp.cc:29)
由于我对C ++很陌生并且我花了很长时间搜索没有任何结果的错误,我想问一个更有经验的人的意见,以了解这个问题是否是由于我引入的一些错误(因为我期待)或编译器错误。
修改: 在Mike Seymour的回答之后,我改变了现在的代码。现在我没有得到编译器警告,但我仍然得到与以前相同的行为(使用相同的valgrind响应)。有没有人试图与英特尔一起编译?
修改: 我试图在维基百科的Expression Templates页面中编译代码。我获得了与我提供的示例完全相同的行为。
修改:
我已经进一步研究了这个问题,似乎用英特尔icpc
编译运算符
operator const Derived&() const {
return static_cast< const Derived& >(*this);
}
以递归方式调用自身。我找到的一个解决方法是用一个方法替换此运算符:
const Derived& get_ref() const {
return static_cast< const Derived& >(*this);
}
并相应地修改各个类的构造函数。任何人都能说出这两种行为中的哪一种可能指向解释它的标准?
答案 0 :(得分:6)
您应始终启用编译器警告;他们经常会发现微妙的问题。在这种情况下:
g++ -Wall -Wextra test.cpp
test.cpp: In member function ‘const typename AlgebraicVectorExpression<AlgebraicVectorSum<T1, T2> >::ValueType& AlgebraicVectorSum<T1, T2>::operator[](typename AlgebraicVectorExpression<AlgebraicVectorSum<T1, T2> >::SizeType) const [with T1 = AlgebraicVector, T2 = AlgebraicVector]’:
test.cpp:90: instantiated from ‘AlgebraicVector::AlgebraicVector(const AlgebraicVectorExpression<T1>&) [with T = AlgebraicVectorSum<AlgebraicVector, AlgebraicVector>]’
test.cpp:103: instantiated from here
test.cpp:52: warning: returning reference to temporary
这告诉你问题:
const ValueType& operator[](SizeType ii) const {
return (a_[ii] + b_[ii]);
}
表达式的结果是临时的,在该行的末尾被销毁,因此该函数返回对不存在的对象的悬空引用。此运算符必须按值返回,并且您不应实现非const
重载,因为没有值可以修改。