473,396 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

partial ordering of overloaded function templates

I've tried the following program on two different compilers (VC++ 7.1 and
gcc 3.3.1) and both print "first" when I expected "second". Why is that?

#include <vector>
#include <iostream>

template <class Iter>
void func(Iter i)
{
std::cout << "first\n";
}

template <class T>
void func(typename std::vector<T>::iterator i)
{
std::cout << "second\n";
}

int main()
{
std::vector<int> v;
func(v.begin());
}

john
Jul 22 '05 #1
5 1589

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2i************@uni-berlin.de...
I've tried the following program on two different compilers (VC++ 7.1 and
gcc 3.3.1) and both print "first" when I expected "second". Why is that?
template <class T>
void func(typename std::vector<T>::iterator i)


OK, I've figured it out, 'std::vector<T>::iterator' is a non-deduced
context. Annoying.

john
Jul 22 '05 #2
John Harrison wrote in news:2i************@uni-berlin.de in comp.lang.c++:
I've tried the following program on two different compilers (VC++ 7.1 and
gcc 3.3.1) and both print "first" when I expected "second". Why is that?

Because the second is non-deducable.

Its non-deducable because the standard says so.
template <class T>
void func(typename std::vector<T>::iterator i)
{
std::cout << "second\n";
}


The good news is: its non-deducable :), which can be exploited:

news:<40**************@bdal.de>

If the news link doesn't work: http://tinyurl.com/2smov

Rob.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3
John Harrison wrote:
I've tried the following program on two different compilers (VC++ 7.1 and
gcc 3.3.1) and both print "first" when I expected "second". Why is that?

#include <vector>
#include <iostream>

template <class Iter>
void func(Iter i)
{
std::cout << "first\n";
}

template <class T>
void func(typename std::vector<T>::iterator i)
{
std::cout << "second\n";
}

int main()
{
std::vector<int> v;
func(v.begin());
}
Function member type resolution you're looking for is not allowed
(because in many cases it is ambiguous).

You *might* be able to do somthing like the example below. This may not
compile on all compilers. I recently found a number of bugs in gcc and
VC++ in this area (Comeau got it right - again!). gcc 3.4.0 appears to
compile this fine.
#include <vector>
#include <list>
#include <iostream>
template <bool v>
struct Helper
{
};

template <typename T1, typename T2>
struct IsSameType
{
static const bool value = false;
};

template <typename T>
struct IsSameType<T, T>
{
static const bool value = true;
};
template <class T>
void func(
const T & v,
Helper<
IsSameType<
T,
typename std::vector<typename T::value_type>::iterator::value
* x

)
{
std::cout << "second\n";
}

struct Y
{
};

template <class Iter>
struct X
: Y
{
};

template <class Iter>
void func(Iter i, ... )
{
std::cout << "first\n";
}

int main()
{
std::vector<int> v;
func(v.begin(), static_cast< Helper<true> * >( 0 ) );
std::list<int> x;
func(x.begin(), static_cast< Helper<true> * >( 0 ) );
}
Jul 22 '05 #4
In article <2i************@uni-berlin.de>,
"John Harrison" <jo*************@hotmail.com> wrote:
"John Harrison" <jo*************@hotmail.com> wrote in message
news:2i************@uni-berlin.de...
I've tried the following program on two different compilers (VC++ 7.1 and
gcc 3.3.1) and both print "first" when I expected "second". Why is that?
template <class T>
void func(typename std::vector<T>::iterator i)


OK, I've figured it out, 'std::vector<T>::iterator' is a non-deduced
context. Annoying.

john


Here is something that you can try, works with Metrowerks:

#include <vector>
#include <iostream>

template <class Iter>
typename Metrowerks::restrict_to
<
!Metrowerks::is_same
<
Iter,
typename std::vector<typename
std::iterator_traits<Iter>::value_type>::iterator::value, void::type func(Iter i)
{
std::cout << "first\n";
return (void)0;
}

template <class Iter>
typename Metrowerks::restrict_to
<
Metrowerks::is_same
<
Iter,
typename std::vector<typename
std::iterator_traits<Iter>::value_type>::iterator::value, void::type

func(Iter i)
{
std::cout << "second\n";
return (void)0;
}

int main()
{
std::vector<int> v;
func(v.begin());
}

Metrowerks::restrict_to is the same things as boost::enable_if (
http://www.boost.org/libs/utility/enable_if.html ).

-Howard
Jul 22 '05 #5

"John Harrison" <jo*************@hotmail.com> wrote in message
news:2i************@uni-berlin.de...
I've tried the following program on two different compilers (VC++ 7.1 and
gcc 3.3.1) and both print "first" when I expected "second". Why is that?

#include <vector>
#include <iostream>

template <class Iter>
void func(Iter i)
{
std::cout << "first\n";
}

template <class T>
void func(typename std::vector<T>::iterator i)
{
std::cout << "second\n";
}

int main()
{
std::vector<int> v;
func(v.begin());
}

john


Thanks for the suggested workarounds. Unfortunately the code I posted bears
little relation to my real code so they aren't applicable, interesting
though. Fortunately though once I realised what the problem was I had a
different way to solve this.

john
Jul 22 '05 #6

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

Similar topics

17
by: Paul MG | last post by:
Hi Template partial specialization always seems like a fairly straightforward concept - until I try to do it :). I am trying to implement the input sequence type (from Stroustrup section...
5
by: Levent | last post by:
Hi, Why doesn't this work? (tried with gcc 3.3.3 and VC++ 7.1): #include <iostream> template<class T, unsigned N> struct Foo { void func(); }; template<class T, unsigned N>
7
by: Kai-Uwe Bux | last post by:
Hi folks, I observed something that puzzles me. When I do namespace xxx { using std::swap; } it appears that xxx::swap and std::swap are not strictly equivalent. In particular, I think...
7
by: Lasse Vågsæther Karlsen | last post by:
I have a list of items and a "rule" for ordering them. Unfortunately, the rule is not complete so it won't define the correct order for any two items in that list. In other words, if I pick...
5
by: Niklas Norrthon | last post by:
I've been banging my head in the wall for some time now over a little problem having to do with partial specialization of function templates. The real problem is more complex than this, but...
6
by: wkaras | last post by:
I tried a couple of compilers, and both gave errors compiling this: template <bool fin, typename T> T foo(T val); template <typename T> T foo<true, T>(T val) { return(val); } But both gave...
9
by: Marek Vondrak | last post by:
Hello. I have written the following program and am curious why it prints "1" "2". What are the exact effects of explicitly providing function template parameters at the call? Is the second...
2
by: desktop | last post by:
If a function should work with different types you normally overload it: void myfun(int a){ // do int stuff } void myfun(std::string str){ // do string stuff }
9
by: Greg | last post by:
Hi, I would like to specify behavior of a class member relatively to template implemetation. It works in usual cases but it seems to fail with to templates when one of the two is specified... ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
0
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.