Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > function template problem

Reply
Thread Tools

function template problem

 
 
John Harrison
Guest
Posts: n/a
 
      06-12-2004
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


 
Reply With Quote
 
 
 
 
Siemel Naran
Guest
Posts: n/a
 
      06-12-2004
"John Harrison" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> 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.


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.


 
Reply With Quote
 
 
 
 
Rob Williscroft
Guest
Posts: n/a
 
      06-12-2004
John Harrison wrote in news:(E-Mail Removed) 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:trdiff_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/
 
Reply With Quote
 
John Harrison
Guest
Posts: n/a
 
      06-12-2004

"Rob Williscroft" <(E-Mail Removed)> wrote in message
news:Xns9506693ABAC63ukcoREMOVEfreenetrtw@130.133. 1.4...
> John Harrison wrote in news:(E-Mail Removed) 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:trdiff_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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
Passing pointer to template function as argument to pointer to template function Vijai Kalyan C++ 4 11-08-2005 07:53 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
Template function as argument to another template function Jim West C++ 3 10-07-2004 07:21 PM
function non-template versus fully specialized function template Robert Allan Schwartz C++ 1 08-09-2004 03:41 PM



Advertisments