具有与具有不同模板参数的其他模板容器交互的多个模板参数的模板容器 [英] Template container with multiple template parameters interacting with other template containers with a different template parameter

查看:111
本文介绍了具有与具有不同模板参数的其他模板容器交互的多个模板参数的模板容器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Wordy标题,是的,但我不确定怎么说。假设我有一个容器类,它有两个模板参数,第一个是类型,第二个是容器的本地存储的大小。

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.

现在我们有多个具有不同容器存储大小的容器。基本上,容器函数(所有的公共的,反正)只真正关心 T ; N 仅用于分配本地存储(如果 N 不足,则使用分配器)。

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;
}

这里是一个IDEONE链接: http://ideone.com/1XKwD

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

使用gcc-4.3.4编译时,

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

当使用VS2010编译时,得到以下结果。

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\testunholytemplatemess\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>          ]

也许我很厚,但我没有解释这些是什么试图告诉我(仍然有些新的模板)。我也不明白为什么运算符[] 方法应该真正关心 N ,或者我在具有不同 N 值的容器上调用 operator [] 如果你将 _other [i] 更改为 _other.Local [i] ,它可以正常工作。

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.

有人有任何建议吗?

推荐答案

] -operator,一个const和一个非const的:

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!

这篇关于具有与具有不同模板参数的其他模板容器交互的多个模板参数的模板容器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆