M
Matthias
Dear newsgroup.
This is my first post here, I hope I'm doing it right.
I wrote a circular iterator class which works as I intended but there
is a little quirk left which maybe you can help me to get rid of:
I want the constructor of my class to accept an iterator range and an
optional current iterator which defaults to the start iterator.
Additionally, it should accept an STL-style container (which will be
used from .begin() to .end()) and an optional current iterator which
defaults to the .begin()-iterator.
The problem is, when I use an array of non-const data to feed my class
(having a pointer to const data as a template argument), the compiler
(GCC) seems to pick the wrong constructor.
I reduced the problem to the following (hopefully minimal) example:
template <typename T>
struct my_class
{
my_class(const T& begin, const T& end, const T& current = T()) :
_begin(begin), _end(end), _current(current != T() ? current :
begin)
{}
template <typename Container> explicit
my_class(Container& container, const T& current = T()) :
_begin(container.begin()), _end(container.end()),
_current(current != T() ? current : _begin)
{}
private:
const T _begin;
const T _end;
T _current;
};
int main()
{
float array[3] = { 1.5, 2.5, 3.5 };
my_class<float*> first(array, array + 3);
// this doesn't work:
my_class<const float*> second(array, array + 3);
// this, however, does:
my_class<const float*> third(&array[0], array + 3);
// even this works:
my_class<const float*> fourth(array + 0, array + 3);
// and with non-const data it also works:
my_class<float*> fifth(array, array + 3);
}
The error message (in the line where "second" is initialized) is:
error: request for member 'begin' in 'container', which is of non-
class type 'float [3]'
The question is, I guess, how can I tell the compiler, that "array" is
not a container but just a pointer to the first array element.
I'm sure I could avoid this special case in my code, but somehow I got
curious ...
Thanks in advance for your help,
Matthias
This is my first post here, I hope I'm doing it right.
I wrote a circular iterator class which works as I intended but there
is a little quirk left which maybe you can help me to get rid of:
I want the constructor of my class to accept an iterator range and an
optional current iterator which defaults to the start iterator.
Additionally, it should accept an STL-style container (which will be
used from .begin() to .end()) and an optional current iterator which
defaults to the .begin()-iterator.
The problem is, when I use an array of non-const data to feed my class
(having a pointer to const data as a template argument), the compiler
(GCC) seems to pick the wrong constructor.
I reduced the problem to the following (hopefully minimal) example:
template <typename T>
struct my_class
{
my_class(const T& begin, const T& end, const T& current = T()) :
_begin(begin), _end(end), _current(current != T() ? current :
begin)
{}
template <typename Container> explicit
my_class(Container& container, const T& current = T()) :
_begin(container.begin()), _end(container.end()),
_current(current != T() ? current : _begin)
{}
private:
const T _begin;
const T _end;
T _current;
};
int main()
{
float array[3] = { 1.5, 2.5, 3.5 };
my_class<float*> first(array, array + 3);
// this doesn't work:
my_class<const float*> second(array, array + 3);
// this, however, does:
my_class<const float*> third(&array[0], array + 3);
// even this works:
my_class<const float*> fourth(array + 0, array + 3);
// and with non-const data it also works:
my_class<float*> fifth(array, array + 3);
}
The error message (in the line where "second" is initialized) is:
error: request for member 'begin' in 'container', which is of non-
class type 'float [3]'
The question is, I guess, how can I tell the compiler, that "array" is
not a container but just a pointer to the first array element.
I'm sure I could avoid this special case in my code, but somehow I got
curious ...
Thanks in advance for your help,
Matthias