4

以下是生成用于添加 N 个向量的表达式模板的代码,但目前我只关注添加两个向量。

template < typename Operation, typename Va>
struct VectorExpression < Operation, Va >
{
    friend Va;
    typedef VectorExpression< Operation, Va> This;
    const Operation& op;
    const Va& vector;

    inline VectorExpression ( Operation oper, const Va& first) : op(oper), vector(first) {};

    inline auto size() const ->decltype( vector.size() ) {
        return vector.size();
    };

    inline auto operator[]( size_t i ) const 
        ->decltype( vector[i] ) {
        return vector[i];
        };
};

template < typename Operation, typename Va, typename ... Vs >
struct VectorExpression <Operation, Va, Vs...> : protected VectorExpression< Operation, Vs...>
{
    friend Va;
    typedef VectorExpression< Operation, Va, Vs...> This;
    typedef VectorExpression< Operation, Vs... > Base;
    const Operation op;
    const Va& vector;

    inline VectorExpression ( Operation oper, const Va& first, const Vs&... vs ) : VectorExpression< Operation, Vs...>(oper, vs...), op(oper), vector(first) {};

    inline auto size() const ->decltype( vector.size() ) {
        return vector.size();
    };

    inline auto operator[]( size_t i ) const //This line is where the error says it is happening
        ->decltype( op( vector[i], Base::operator[](i) ) ) {
        return op( vector[i], Base::operator[](i) );
        };
};

不幸的是,当我尝试编译前面的内容时,我收到以下错误消息:

error: cannot call member function 'decltype (((const VectorExpression<Operation, Va>*)this)->VectorExpression<Operation, Va>::vector[i]) 
    VectorExpression<Operation, Va>::operator[](size_t) const 
    [with 
        Operation = adder<std::complex<double>, double>; 
        Va = Vector<double, false>; 
        decltype (((const VectorExpression<Operation, Va>*)this)->VectorExpression<Operation, Va>::vector[i]) = const double&; size_t = long unsigned int]'  without object

我不确定问题出在哪里。这似乎是一个相当简单的递归定义operator[],将当前对象中的向量与所有基类向量相加。

有任何想法吗?我不相信其余的代码是相关的[编辑:但是可以在此处找到标头,并且可以在此处找到测试“main” ,但是如果您想查看它,请告诉我。此外,C++11 中是否有任何方法可以在位置打印出类型以调试此类问题?

4

1 回答 1

6

你不能这样做Base::operator[](i)decltype因为它不在成员函数的主体中;你必须做this->Base::operator[](i)

于 2013-03-10T22:04:27.773 回答