J
John Harrison
This might be a compiler problem or it might be some subtlety of the
language I don't understand.
I'm writing an iterator and I want to write a version of std::distance
specially for my iterator. Here's some code (highly reduced of course). The
issue is how the return type of std::distance should be written.
#include <iostream>
#include <cstddef>
#include <iterator>
template <class T>
class Iter
{
public:
typedef std::input_iterator_tag iterator_category;
typedef T value_type;
typedef T& reference;
typedef T* pointer;
typedef std::size_t size_type;
typedef std:
trdiff_t difference_type;
bool operator==(Iter rhs) const { return true; }
bool operator!=(Iter rhs) const { return false; }
Iter& operator++() { return *this; }
Iter operator++(int) { return *this; }
};
namespace std
{
template <class T>
typename std::iterator_traits<Iter<T> >::distance_type /* return type 1 */
// std:
trdiff_t /* return type 2 */
distance(Iter<T> first, Iter<T> last)
{
std::cout << "success\n";
return 0;
}
}
int main()
{
std::distance(Iter<int>(), Iter<int>());
}
On MSVC++ 7.1 this prints "success" proving that my version of std::distance
has been called. On gcc 3.3.1 it does not. However if I change the return
type of my version std::distance to std:
trdiff_t, then gcc 3.3.1 prints
"success" as well.
I was pretty much sure this was a gcc compiler bug but I thought I'd test
with Comeau C++ online, from what I've been able to tell Comeau works the
same as gcc, so now I'm not so sure.
Anyone able to tell me what's going on?
John
language I don't understand.
I'm writing an iterator and I want to write a version of std::distance
specially for my iterator. Here's some code (highly reduced of course). The
issue is how the return type of std::distance should be written.
#include <iostream>
#include <cstddef>
#include <iterator>
template <class T>
class Iter
{
public:
typedef std::input_iterator_tag iterator_category;
typedef T value_type;
typedef T& reference;
typedef T* pointer;
typedef std::size_t size_type;
typedef std:
bool operator==(Iter rhs) const { return true; }
bool operator!=(Iter rhs) const { return false; }
Iter& operator++() { return *this; }
Iter operator++(int) { return *this; }
};
namespace std
{
template <class T>
typename std::iterator_traits<Iter<T> >::distance_type /* return type 1 */
// std:
distance(Iter<T> first, Iter<T> last)
{
std::cout << "success\n";
return 0;
}
}
int main()
{
std::distance(Iter<int>(), Iter<int>());
}
On MSVC++ 7.1 this prints "success" proving that my version of std::distance
has been called. On gcc 3.3.1 it does not. However if I change the return
type of my version std::distance to std:
"success" as well.
I was pretty much sure this was a gcc compiler bug but I thought I'd test
with Comeau C++ online, from what I've been able to tell Comeau works the
same as gcc, so now I'm not so sure.
Anyone able to tell me what's going on?
John