这与本文下方提出的问题略有不同.假设我有一个容器类,它有两个模板参数,第一个是类型,第二个是容器的大小.
现在我们有多个具有不同容器存储大小的容器.从本质上讲,容器功能(所有公共功能)只是真正关心T; N仅用于分配本地存储(如果N不够,则使用分配器).
我已经汇总了一个简单的示例实现,展示了我遇到的问题.
#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;
}
bool operator != (const Iterator& _other) const
{
return !(*this == _other);
}
template <size_t _N>
Iterator& operator = (const typename TestArray<T, _N>::Iterator &_other)
{
Array = _other.Array;
Index = _other.Index;
return *this;
}
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.Local[i];
}
};
Run Code Online (Sandbox Code Playgroud)
这实际上是一个两部分问题.我之前发布的第一部分:具有多个模板参数的模板容器与具有不同模板参数的其他模板容器交互.对于第二部分,我试图使用它如下:
int main() {
TestArray<int> testArray1;
TestArray<int, 25> testArray2;
TestArray<int>::Iterator itr;
itr = testArray1.Begin();
for (itr = testArray1.Begin(); itr != testArray1.End(); itr.Next())
{
itr.Get() = itr1.Index;
}
testArray2.Copy(testArray1, 0, 10);
for (itr = testArray2.Begin(); itr != testArray2.End(); itr.Next())
{
std::cout << itr.Get() << std::endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这是一个IDEONE链接:http://ideone.com/GlN54
使用gcc-4.3.4编译时,我得到以下内容:
prog.cpp: In function ‘int main()’:
prog.cpp:67: error: no match for ‘operator=’ in ‘itr = testArray2.TestArray<T, N>::Begin [with T = int, unsigned int N = 25u]()’
prog.cpp:10: note: candidates are: TestArray<int, 10u>::Iterator& TestArray<int, 10u>::Iterator::operator=(const TestArray<int, 10u>::Iterator&)
prog.cpp:67: error: no match for ‘operator!=’ in ‘itr != testArray2.TestArray<T, N>::End [with T = int, unsigned int N = 25u]()’
prog.cpp:19: note: candidates are: bool TestArray<T, N>::Iterator::operator!=(const TestArray<T, N>::Iterator&) const [with T = int, unsigned int N = 10u]
Run Code Online (Sandbox Code Playgroud)
在VS2010中,我得到以下内容:
1>------ Build started: Project: testunholytemplatemess, Configuration: Debug Win32 ------
1> main.cpp
1>c:\users\james\documents\testproj\testunholytemplatemess\testunholytemplatemess\main.cpp(67): error C2679: binary '=' : no operator found which takes a right-hand operand of type 'TestArray<T,N>::Iterator' (or there is no acceptable conversion)
1> with
1> [
1> T=int,
1> N=25
1> ]
1> c:\users\james\documents\testproj\testunholytemplatemess\testunholytemplatemess\main.cpp(34): could be 'TestArray<T>::Iterator &TestArray<T>::Iterator::operator =(const TestArray<T>::Iterator &)'
1> with
1> [
1> T=int
1> ]
1> while trying to match the argument list '(TestArray<T>::Iterator, TestArray<T,N>::Iterator)'
1> with
1> [
1> T=int
1> ]
1>c:\users\james\documents\testproj\testunholytemplatemess\testunholytemplatemess\main.cpp(67): error C2679: binary '!=' : no operator found which takes a right-hand operand of type 'TestArray<T,N>::Iterator' (or there is no acceptable conversion)
1> with
1> [
1> T=int,
1> N=25
1> ]
1> c:\users\james\documents\testproj\testunholytemplatemess\testunholytemplatemess\main.cpp(19): could be 'bool TestArray<T>::Iterator::operator !=(const TestArray<T>::Iterator &) const'
1> with
1> [
1> T=int
1> ]
1> while trying to match the argument list '(TestArray<T>::Iterator, TestArray<T,N>::Iterator)'
1> with
1> [
1> T=int
1> ]
Run Code Online (Sandbox Code Playgroud)
我认为Iterator& operator =这样做会使这个赋值运算符起作用,但显然不行.也许我很厚,但我没有在这里确定正确的解决方案.有没有人有什么建议?
TestArray<T, 1>并且TestArray<T, 2>是不同的类型,所以TestArray<T, 1>::Iterator和TestArray<T, 2>::Iterator.作业无法奏效!(您itr的类型与其类型不同testArray2.Begin().)
整个建筑对我来说似乎很可疑 - 这真的有必要吗?你想要实现什么目标?你看过了std::array吗?
更新:如果您明确提供模板参数,它会起作用:
for (itr.operator=<int,25>(testArray2.Begin());
itr.operator!=<int,25>(testArray2.End());
itr.Next())
{
std::cout << itr.Get() << std::endl;
}
Run Code Online (Sandbox Code Playgroud)
我不完全确定为什么参数不能从参数推断出来,我期待一个很好的解释 - 同时,我把完整的代码放在Ideone上,尽管它不在那里编译,但它确实与我的GCC 4.6.1.