0

我正在尝试使用来自 boost 的 uBlas 矩阵作为后端来实现一个三维张量。其中一个功能是获取对切片的引用并可以轻松分配矩阵。

下面是张量类的一个片段:

template<class L, class M>
class tensor {
public:
    typedef L layout_type;
    typedef std::size_t size_type;
    typedef M array_type;

private:
    size_type size1_;
    size_type size2_;
    size_type size3_;

    array_type data_;
public:

    /**
     * Return a constant reference to the internal storage of a dense tensor, i.e. the raw data
     * It's type depends on the type used by the tensor to store its data
     */
    BOOST_UBLAS_INLINE
    const array_type &data() const {
        return data_;
    }
    /**
     * Return a reference to the internal storage of a dense tensor, i.e. the raw data
     * It's type depends on the type used by the tensor to store its data
     */
    BOOST_UBLAS_INLINE
    array_type &data() {
        return data_;
    }

    /**@}*/

    /**
     * @name Slices' access
     * Accessors for slices across each dimension
     */
    /**@{*/
    BOOST_UBLAS_INLINE
    typename layout_type::template slice<1>::matrix_slice_type 
    at_dim1_slice(uint32_t i) {
        return ublas::trans(ublas::project(data(), layout_type::template slice<1>::coord1(i, size1_, size2_, size3_),
                layout_type::template slice<1>::coord2(i, size1_, size2_, size3_)));

    }
}

layout_type 看起来像:

template<class M>
struct basic_dim2_major {
    typedef M matrix_type;

    template<int DIM, class DUMMY = void>
    struct slice {
    };
    template<class DUMMY> struct slice<1, DUMMY> {

        struct trans {
            template<class T>
            auto operator()(T &x) ->decltype(ublas::trans(x))
            {
                return ublas::trans(x);
            }
        };

        typedef ublas::matrix_slice<matrix_type> matrix_slice_type;
        typedef typename std::result_of<trans(matrix_slice_type&)>::type Type;

        static BOOST_UBLAS_INLINE
        Type
        orient(matrix_slice_type &data){
            return ublas::trans(data);
        }

        static BOOST_UBLAS_INLINE
        ublas::slice coord1(size_type i, size_type size_i, size_type size_j, size_type size_k) {
            return ublas::slice(i, size_i, size_k);
        }
        static BOOST_UBLAS_INLINE
        ublas::slice coord2(size_type i, size_type size_i, size_type size_j, size_type size_k) {
            return ublas::slice(0, 1, size_j);
        }
     }
} 

用例如下:

ublas::matrix slice1(3,4);
tensor<> t(2,3,4);
t.at_dim1_slice(0) = slice1;

此行存在问题:

    return ublas::trans(ublas::project(data(), layout_type::template slice<1>::coord1(i, size1_, size2_, size3_),
        layout_type::template slice<1>::coord2(i, size1_, size2_, size3_)));

当 trans 和 project 函数一起使用时,编译器会选择 project 和 trans 的 const 重载,我不能像上面那样进行赋值。但是,如果我只留下项目,则使用非常量方法并且一切正常。不幸的是,由于设计的存储布局(映射到二维矩阵),切片的转置是必要的。

const matrix_slice<const M> project (const M &data, const typename matrix_slice<M>::slice_type &s1, const typename matrix_slice<M>::slice_type &s2);
matrix_slice<M> project (matrix_slice<M> &data, const typename matrix_slice<M>::slice_type &s1, const typename matrix_slice<M>::slice_type &s2);

是否有任何解决方案来指示正确的功能重载?或者我在某处犯了错误?

4

1 回答 1

0

当 const 和非 const 版本可用时const,如果对象本身是 const,则编译器会选择版本。如果对象是非常量,则编译器选择非常量版本。函数就是这样解决的。

所以在你的代码中,如果选择了 const 版本,那么对象必须是 const 本身。

另请注意,在 const 成员函数中,this是一个 const 指针,并且从它返回的任何成员数据也是const.

于 2011-08-26T02:40:01.647 回答