By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,560 Members | 2,989 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,560 IT Pros & Developers. It's quick & easy.

Is This Legal Template Code? (Deduction Rules(?))

P: n/a
Greetings all:

I come across an interesting question (to me anyway) and I do not
know the answer. Code/Questions follow:

#include <iostream>

#if 0
// uncommenting *should* make call ambigous because of
// odering deduction of the function signature,
// not the type of the parameter
template<typename T>
void t(T*, T const* = 0, ...) {}
#endif

// this is essentially the code presented in an earlier question
// IRC that I stumbled on; gcc-3.2.2 compiles
// doesn't denote that it's *correct* though
template<typename T>
void t(T const*, T*, T* = 0)
{ std::cout << "template func with one template parameter\n";}

// signature match previous declaration of 't' so now it's on to
// 'template' ordering rules of 'type parameter' at this point for
// the compiler
template<typename T, typename C>
void t(C const*, C*, C* = 0)
{ std::cout << "template func with two template parameters\n"; }

void example(int* p)
{
// calls t<T,C>
t<int *>(p, p);
// calls t<T>
t<>(p, p);
}

int main(int argc, char** argv)
{
example(&argc);

return 0;
}

The crux of my problem is the two seperate 'void t(...)' definitions.
That is the part I am having trouble with.

If this is legal can someone tersely explain why it works? I will do
my own homework on coverting the terse to verbose. ;)

If is not legal code then the obvious theory is I'm using a broken
compiler! - This conclusion based on the fact it compiles and excutes
instead of a compilation error for an ambigous declaration.

For what it's worth my first reaction is this should still be an
ambiguous call therefore not legal. Any input on this will be greatly
appreciated in advance. Thanks!

C Johnson

P.S. Given the noise level ealier in this group - I hope my question
is on topic and in the appropriate newsgroup! It's a joke - laugh ;)

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----
Jul 19 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Chris Johnson wrote:
The crux of my problem is the two seperate 'void t(...)' definitions.
That is the part I am having trouble with.

If this is legal can someone tersely explain why it works? I will do
my own homework on coverting the terse to verbose. ;)

If is not legal code then the obvious theory is I'm using a broken
compiler! - This conclusion based on the fact it compiles and excutes
instead of a compilation error for an ambigous declaration.


Comeau online complains that it isnt legal.You can try it
out here yourself:

http://www.comeaucomputing.com/tryitout/

Christoph

Jul 19 '05 #2

P: n/a
On 13 Aug 2003 06:02:49 -0700, de*********@excite.com (C Johnson)
wrote:
So here it is as follows with the same questions as my original post:

#include <iostream>

template<typename T>
void t(T const*, T*, T* = 0)
{ std::cout << "template func with one template parameter\n";}

template<typename T, typename C>
void t(C const*, C*, C* = 0)
{ std::cout << "template func with two template parameters\n"; }

void example(int* p)
{
// calls t<T,C>
t<int *>(p, p);
Here, TAD (template argument deduction) gives you only 1 possibility:

t<int*, int>(int const*, int*, int*)

There is no way to match the first template function (it could match
t<int>(int const*, int*, int*), but not t<int*>(anything))
// calls t<T>
t<>(p, p);


Again, TAD gives us only one choice:

t<int>(int const*, int*, int*)

The second template function cannot be chosen since T is non-deducable
(it doesn't appear in any parameter).

So it isn't ambiguous. A more interesting case is this call:

t<int>(p, p);

Now, TAD successfully deduces signatures for both templates:

1: t<int>(int const*, int*, int*)
2: t<int, int>(int const*, int*, int*)

Partial ordering chooses the second since it is more specialized
(mainly because it has a non-deducable extra template parameter).

Tom
Jul 19 '05 #3

P: n/a
to********@hotmail.com (tom_usenet) wrote in message news:<3f****************@news.easynet.co.uk>...
[...]
So it isn't ambiguous. A more interesting case is this call:

t<int>(p, p);

Now, TAD successfully deduces signatures for both templates:

1: t<int>(int const*, int*, int*)
2: t<int, int>(int const*, int*, int*)

Partial ordering chooses the second since it is more specialized
(mainly because it has a non-deducable extra template parameter).

Tom


Well I feel very silly for not being able to figure that out on my
own. D'oh! Thanks for the insight.

C Johnson
Jul 19 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.