473,806 Members | 2,259 Online
Bytes | Software Development & Data Engineering Community
+ 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_iter ator_tag iterator_catego ry;
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_t raits<Iter<T> >::distance_typ e /* 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(I ter<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 1710
"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_iter ator_tag iterator_catego ry;
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_t raits<Iter<T> >::distance_typ e /* 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(I ter<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_t raits<Iter<T> >::distance_typ e /* 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_i terator_tag iterator_catego ry;
};

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_t raits<Iter<T> >::distance_typ e /* 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_i terator_tag iterator_catego ry;
};

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
5076
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 offered me a kind explanation. TIA Function template and specialization below works fine: template <typename T> T maximum (T a, T b) {
1
1749
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 Studio .NET. The problem is that for gcc the template functions (members of a class) are explicitly instantiated in a cpp file that includes the classes .h and .cpp file. The VS compiler does not correctly bring together the function template...
6
4062
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). // Integer calculation (fast) int iround(const double x) { return (x>=0 ? static_cast<int>(x + 0.5) : static_cast<int>(x + 0.5) } // Floating point calculation (slow) double dround(const double x) {
4
2253
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++) *(arrayPtr+i)*=2;
11
2494
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: class A {}; template <typename T> struct Is_C { static const bool value = false;
7
354
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 b){ return (a + b); } it can only deal with int + int or double + double however, I also want int + double, double + int so I come up with this idea template<class T , class U> T addition(T a, U b){ return (a+b); } this created a...
3
2924
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 T,typename Alloc = std::allocator<T class CircularBuffer4 { public: typedef typename CircularBuffer<T,Alloc>::size_type
16
3961
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
2312
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 switch statements. My first approach was to rely on partial template specialization. Therefore, I have: // .h file template <int vSomeClass;
4
2300
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 complains about the fact that the function foo (which is declared as a template) is not a template! The problem itself vanishes when changing the name of the function foo in the class bar into some other function name, lets say goo. The problem...
0
9718
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10617
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10364
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10370
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10109
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5678
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3849
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3008
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.