开发者

Template container with multiple template parameters interacting with other template containers with a different template parameter

开发者 https://www.devze.com 2023-03-24 03:50 出处:网络
Wordy title, yes, but I was unsure how else to say it.Suppose I have a container class which has two template parameters, the first of which is a type, the second of which is the size of the local sto

Wordy title, yes, but I was unsure how else to say it. Suppose I have a container class which has two template parameters, the first of which is a type, the second of which is the size of the local storage for the container.

Now we have multiple containers with a different container storage size. Essentially, the container functions (all the public ones, anyway) only really care about T; N is only used to allocate local storage (an allocator is used if N is not enough).

I have put together a simple example implementation that showcases the problem I am having.

#include <iostream>

template <typename T, size_t N = 10>
class TestArray
{
public:
    T Local[N];

    class Iterator
    {
    public:
        T* Array;
        int Index;      

        Iterator() : Array(NULL), Index(-1) { }
        Iterator(T* _array, int _index) : Array(_array), Index(_index) { }

        bool operator == (const Iterator& _other) const 
        { 
             return _other.Index == Index && _other.Array == Array; 
        }

        void Next() { ++Index; }
        void Prev() { --Index; }

        T& Get() { return Array[Index]; }
    };

    T& operator [] (const int _index) { return Local[_index]; }

    Iterator Begin() { return Iterator(Local, 0); }
    Iterator End() { return Iterator(Local, N); }

    template <size_t _N>
    void Copy(const TestArray<T, _N> &_other, int _index, int _count)
    {   
        int i;

        for (i = 0; i < _count; i++)
            Local[_index + i] = _other[i];
    }   
};

This is really a two part question. I will concern this question only with the first part, and ask another regarding the second. I tried using it as follows:

int main() {

    TestArray<int> testArray1;
    TestArray<int, 25> testArray2;

    TestArray<int>::Iterator itr1;
    TestArray<int, 25>::Iterator itr2;

    itr1 = testArray1.Begin();

    for (itr1 = testArray1.Begin(); itr1 != testArray1.End(); itr1.Next())
    {
        itr1.Get() = itr1.Index;
    }

    testArray2.Copy(testArray1, 0, 10);

    for (itr2 = testArray2.Begin(); itr2 != testArray2.End(); itr2.Next())
    {
        std::cout << itr2.Get() << std::endl;
    }

    return 0;
}

Here is an IDEONE link: http://ideone.com/1XKwD

When compiled with gcc-4.3.4, I get the following.

prog.cpp: In member function ‘void TestArray<T, N>::Copy(const TestArray<T, _N>&, int, int) [with unsigned int _N = 10u, T = int, unsigned int N = 25u]’:
prog.cpp:82:   instantiated from here
prog.cpp:63: error: passing ‘const TestArray<int, 10u>’ as ‘this’ argument of ‘T& TestArray<T, N>::operator[](int) [with T = int, unsigned int N = 10u]’ discards qualifiers

When compiled with VS2010, I get the following.

1>------ Build started: Project: testunholytemplatemess, Configuration: Debug Win32 ------
1>  main.cpp
1>c:\users\james\documents\testproj\testunholytemplatemess\testunholytemplatemess\main.cpp(63): error C2678: binary '[' : no operator found which takes a left-hand operand of type 'const TestArray<T>' (or there is no acceptable conversion)
1>          with
1>          [
1>              T=int
1>          ]
1>          c:\users\james\documents\testproj\testunholytemplatemess\testunholytemplatemess\main.cpp(44): could be 'int &TestArray<T>::operator [](const int)'
1>          with
1>          [
1>              T=int
1>          ]
1>          while trying to match the argument list '(const TestArray<T>, int)'
1>          with
1>          [
1>              T=int
1>          ]
1>          c:\users\james\documents\testproj\testunholytempla开发者_如何学Gotemess\testunholytemplatemess\main.cpp(82) : see reference to function template instantiation 'void TestArray<T,N>::Copy<10>(const TestArray<T> &,int,int)' being compiled
1>          with
1>          [
1>              T=int,
1>              N=25
1>          ]

Maybe I'm being thick, but I'm failing to interpret what either of these is actually trying to tell me (still somewhat new to templates). I also fail to understand why the operator [] method should really care about N, or the fact that I'm calling operator [] on a container with a different N value. If you change _other[i] to _other.Local[i], it works fine.

Does anyone have any suggestions?


You have to overload two versions for the []-operator, a const one and a non-const one:

T & operator [] (size_t _index) { return Local[_index]; }
const T & operator [] (size_t _index) const { return Local[_index]; }

Your constant Copy function is only allowed to use the second, constant version!

0

精彩评论

暂无评论...
验证码 换一张
取 消