Template container with contained class definition that only needs one of multiple template parameters
This is slightly different than the question posed just below this one. 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 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;
}
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];
}
};
This is really a two part question. The first part of which I posted earlier: Template container with multiple template parameters interacting with other template containers with a different template parameter. For the second part, I'm trying to use it as follows:
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;
}
Here is an IDEONE link: http://ideone.com/GlN54
When compiled with gcc-4.3.4, I get the following:
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]
In 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(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开发者_StackOverflow 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> ]
I thought the Iterator& operator =
would make it so that this assignment operator should work, but apparently not. Perhaps I am being thick but I am failing to determine the correct solution here. Does anyone have any suggestions?
TestArray<T, 1>
and TestArray<T, 2>
are different types, and so are TestArray<T, 1>::Iterator
and TestArray<T, 2>::Iterator
. The assignment cannot work! (Your itr
is a different type from the type of testArray2.Begin()
.)
The entire construction seems very dubious to me -- is this really necessary? What are you trying to achieve? Have you looked at std::array
?
Update: It works if you supply the template parameters explicitly:
for (itr.operator=<int,25>(testArray2.Begin());
itr.operator!=<int,25>(testArray2.End());
itr.Next())
{
std::cout << itr.Get() << std::endl;
}
I'm not entirely sure why the parameters cannot be deduced from the arguments, and I am looking forward to a good explanation -- in the meantime, I put the full code on Ideone, though it doesn't compile there, but it does with my GCC 4.6.1.
When you use default template arguments, the compiler simply automatically supplies the defaults, but they are still part of the template definition. In your case, TestArray<int>
is the same as TestArray<int, 10>
. If you want it to inter-operate between TestArray<T, N> and TestArray<T, M>
, you'll probably want to use template functions.
Edit: The only solution I've been able to come up with is to break the iterator out to it's own class, then it works as desired. What I can't tell you is why it doesn't work. My guess is it's because it doesn't like searching for the inner classes for possible template expansions. Either way, here's what I did.
#include <iostream>
template <typename T, size_t N = 10> class TestArray;
template<typename T, size_t N>
class TIterator
{
public:
T* Array;
int Index;
TIterator() : Array(NULL), Index(-1) { }
TIterator(T* _array, int _index) : Array(_array), Index(_index) { }
bool operator == (const TIterator& _other) const
{
return _other.Index == Index && _other.Array == Array;
}
bool operator != (const TIterator& _other) const
{
return !(*this == _other);
}
TIterator& operator = (const TIterator& _other) {
Array = _other.Array;
Index = _other.Index;
return *this;
}
template <size_t M>
bool operator == (const TIterator<T, M>& _other) const
{
return _other.Index == Index && _other.Array == Array;
}
template <size_t M>
bool operator != (const TIterator<T, M>& _other) const
{
return !(*this == _other);
}
template< size_t M>
TIterator& operator = (const TIterator<T, M>& _other) {
Array = _other.Array;
Index = _other.Index;
}
void Next() { ++Index; }
void Prev() { --Index; }
T& Get() { return Array[Index]; }
};
template <typename T, size_t N>
class TestArray
{
public:
T Local[N];
typedef TIterator<T, N> Iterator;
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];
}
};
int main() {
TestArray<int> testArray1;
TestArray<int, 25> testArray2;
TestArray<double, 10> testArray3;
TestArray<int>::Iterator itr;
itr = testArray1.Begin();
for (itr = testArray1.Begin(); itr != testArray1.End(); itr.Next())
{
itr.Get() = itr.Index;
}
testArray2.Copy(testArray1, 0, 10);
for (itr = testArray2.Begin(); itr != testArray2.End(); itr.Next())
{
std::cout << itr.Get() << std::endl;
}
return 0;
}
Note, I had TIterator
use both type arguments, even though it didn't technically use the second template argument, just to show that it worked.
精彩评论