Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ C++;具有常量正确性的自定义数组切片类_C++_Arrays_Constants_Slice - Fatal编程技术网

C++ C++;具有常量正确性的自定义数组切片类

C++ C++;具有常量正确性的自定义数组切片类,c++,arrays,constants,slice,C++,Arrays,Constants,Slice,我正在尝试创建一个自定义数组切片类,该类可以处理任意的、可能是负的跨步,我想知道如何使用const正确性来实现它: #include <vector> #include <cstdio> template< typename T > class ArrayView { public: ArrayView( void* pointer, int strideInBytes, int length ) { m_pPointer

我正在尝试创建一个自定义数组切片类,该类可以处理任意的、可能是负的跨步,我想知道如何使用const正确性来实现它:

#include <vector>
#include <cstdio>

template< typename T >
class ArrayView
{
public:

    ArrayView( void* pointer, int strideInBytes, int length )
    {
        m_pPointer = reinterpret_cast< unsigned char* >( pointer );
        m_strideInBytes = strideInBytes;
        m_length = length;
    }

    const T& operator [] ( int index ) const
    {
        const unsigned char* p = m_pPointer + index * m_strideInBytes;
        return *( reinterpret_cast< const T* >( p ) );
    }

    T& operator [] ( int index )
    {
        unsigned char* p = m_pPointer + index * m_strideInBytes;
        return *( reinterpret_cast< T* >( p ) );
    }

    int length() const
    {
        return m_length;
    }

private:

    unsigned char* m_pPointer;
    int m_strideInBytes;
    int m_length;

};

void printFromReadOnlyView( const ArrayView< int >& view )
{
    for( int i = 0 ; i < view.length(); ++i )
    {
        printf( "%d: %d\n", i, view[i] );
    }
}

void useRef( std::vector< int >& ref )
{
    // works fine
    ArrayView< int > view( ref.data(), sizeof( int ), ref.size() );
    printFromReadOnlyView( view );
}

void useConstRef( const std::vector< int >& constRef )
{
    // const error
    ArrayView< int > view( constRef.data(), sizeof( int ), constRef.size() );
    printFromReadOnlyView( view );
}

int main()
{
    std::vector< int > v( 10 );
    for( int i = 0; i < v.size(); ++i )
    {
        v[i] = i;
    }

    useRef( v );
    useConstRef( v );
}
#包括
#包括
模板
类阵列视图
{
公众:
ArrayView(void*指针,整数跨步字节,整数长度)
{
m_pPointer=重新解释强制转换(指针);
m_stridinbytes=stridinbytes;
m_长度=长度;
}
常量T&运算符[](整数索引)常量
{
常量无符号字符*p=m_指针+索引*m_跨步字节;
返回*(重新解释铸造(p));
}
T&运算符[](整数索引)
{
无符号字符*p=m_指针+索引*m_跨步字节;
返回*(重新解释铸造(p));
}
int length()常量
{
返回m_长度;
}
私人:
无符号字符*m_指针;
int m_,以字节为单位;
int m_长度;
};
无效打印自只读视图(const ArrayView&view)
{
对于(int i=0;i&ref)
{
//很好
ArrayView视图(ref.data(),sizeof(int),ref.size());
从只读视图(视图)打印;
}
void useConstRef(const std::vector&constRef)
{
//常数误差
ArrayView视图(constRef.data(),sizeof(int),constRef.size());
从只读视图(视图)打印;
}
int main()
{
std::vectorv(10);
对于(int i=0;i
函数useConstRef()会导致编译时错误,因为constRef.data()是常量指针,而ArrayView的构造函数接受非常量指针

本质上,我希望ArrayView是常量(只读)或非常量(读写)


我可以尝试添加一个接受常量指针的构造函数,但是我必须存储常量指针并记住我是哪个版本。我可以只使用一个类吗,或者我必须分别创建ReadWriteArrayView和ReadOnlyArrayView版本吗?

ArrayView
ArrayView
应该是两种不同的类型…+1到K,我很确定您会发现const data()指针和构造的非常量void指针不会摇摆。只是出于好奇:你看过
std::valarray
std::slice
等吗?我看过,但我确实需要更灵活的东西。我计划通过多维数组进行一些有趣的非对齐迭代。