473,473 Members | 1,965 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

function template problem

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::ptrdiff_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::ptrdiff_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::ptrdiff_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
Jul 22 '05 #1
3 1697
"John Harrison" <jo*************@hotmail.com> wrote in message
news:2i************@uni-berlin.de...
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::ptrdiff_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::ptrdiff_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::ptrdiff_t, then gcc 3.3.1 prints
"success" as well.
MSVC 7 is right.

As an aside, I think you can define function distance in the same namespace
as class Iter. Koening lookup ensures that a call distance(iter1, iter2)
finds distance in the namespace where iter1 and iter2 live, and then in the
current namespace. But of course, people may write std::distance(...) which
forces the use of the function in the std namespace. So what you have still
seems a good idea.
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?


There is a g++ newsgroup out there. Might be a bug.
Jul 22 '05 #2
John Harrison wrote in news:2i************@uni-berlin.de in
comp.lang.c++:
This might be a compiler problem or it might be some subtlety of the
language I don't understand.
typedef std::ptrdiff_t difference_type;
template <class T>
typename std::iterator_traits<Iter<T> >::distance_type /* return type
Anyone able to tell me what's going on?


SFINAE:

template<class T> struct iterator_traits<T*> {
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef T* pointer;
typedef T& reference;
typedef random_access_iterator_tag iterator_category;
};

There is no distance_type, change to difference_type.
Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3

"Rob Williscroft" <rt*@freenet.co.uk> wrote in message
news:Xn**********************************@130.133. 1.4...
John Harrison wrote in news:2i************@uni-berlin.de in
comp.lang.c++:
This might be a compiler problem or it might be some subtlety of the
language I don't understand.

typedef std::ptrdiff_t difference_type;


template <class T>
typename std::iterator_traits<Iter<T> >::distance_type /* return type


Anyone able to tell me what's going on?


SFINAE:

template<class T> struct iterator_traits<T*> {
typedef ptrdiff_t difference_type;
typedef T value_type;
typedef T* pointer;
typedef T& reference;
typedef random_access_iterator_tag iterator_category;
};

There is no distance_type, change to difference_type.


Aarrgh!! MSVC++ has distance_type for backwards compatibility.

Thanks, and well spotted, everything works now.

john
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: CoolPint | last post by:
While I was testing my understanding of Functioin Template features by playing with simple function templates, I got into a problem which I cannot understand. I would be very grateful if someone...
1
by: Arne Petersen | last post by:
Hy, I've got a problem with member function templates compiled into libraries. I'm trying to get a library collection (coded for GNU gcc, where its compiled completly) being compiled on Visual...
6
by: Jef Driesen | last post by:
I need to implement a function to implement the rounding of floating point values. At the moment i have two different implementations, depending on the type of the return value (integer or double)....
4
by: firegun9 | last post by:
Hello everyone, here is my program: /////////////////////////////// #include <iostream> using namespace std; void multi(double* arrayPtr, int len){ for(int i=0; i<len; i++)...
11
by: gao_bolin | last post by:
I am facing the following scenario: I have a class 'A', that implements some concept C -- but we know this, not because A inherits from a virtual class 'C', but only because a trait tell us so: ...
7
by: hopangtsun | last post by:
Hi all, I have encountered a problem on using the function template my goal is to add two numbers, which they can be int or double if I do this in this way template<class T> T addition(T a, T...
3
by: toton | last post by:
Hi, I want to specialize template member function of a template class . It is creating some syntax problem .... Can anyone say how to do it ? The class is something like this template<typename...
16
by: PengYu.UT | last post by:
Hi, I want to partial specialize the member function doit. But it doesn't work. Could you please help me to figure out what is wrong? Thanks, Peng template <typename T> class A {
12
by: aaragon | last post by:
I have this scenario: several arrays for which I have their fixed values at compilation time. Now, at runtime I need to access a specific array depending on an integer but I want to avoid if and...
4
by: ciccio | last post by:
Dear all, once again I stumbled upon the following puzzling problem. When having the following two files (see below), the gnu compiler compiles the file without a problem while the compiler...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.