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

C

Chris Johnson

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 ;)
 
C

Christoph Rabel

Chris said:
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
 
T

tom_usenet

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 said:
// 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
 
C

C Johnson

[email protected] (tom_usenet) wrote in message news: said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top