472,973 Members | 2,083 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

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

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
3 2154
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Steve | last post by:
Hi all, Is template code "always" inlined??? Or will compilers instantiate template functions separately if they are large? Also, what happens to the code if explicit instantiation mechanism...
3
by: BigMan | last post by:
Here is a piece of code: #include <memory> using namespace std; template< typename SomeType > void f(auto_ptr_ref< SomeType >) { }
1
by: Peng Yu | last post by:
Hi All, In the book Working Paper for Draft Proposed International Standard for Information Systems$)A!* Programming Language C+ +, template argument deduction is discussed. However, the...
14
by: Bart Samwel | last post by:
Hi everybody, I would really like some help explaining this apparent discrepancy, because I really don't get it. Here is the snippet: void foo(int&); void foo(int const&); ...
4
by: George | last post by:
Dear All, I'm compiling the code below with IBM's xlC 6.0 and get the message, "rmspace.cpp", line 34.48: 1540-0298 (S) Template argument deduction cannot be performed using the function...
7
by: Dilip | last post by:
This is just an academic question but is there any particular reason why this does not work? template<typename T> class Foo { public: Foo(T x) : myvar_(x) { } private: T myvar_;
0
by: Jack | last post by:
Hi, I want to override an overridable method in some base class from my class. It works fine if I manually code the method (as one would expect), but I want the IDE to auto-generate the template...
4
by: KD | last post by:
I have a template function and I'm looking for a way to force the caller to specify the template parameters. In other words, I would like to turn off template parameter deduction. For example, ...
0
by: Thelma Roslyn Lubkin | last post by:
dizzy <dizzy@roedu.netwrote: : Hello : When working with template heavy code one will have to face the : inherent "problems" of the inclusion model because of the requirement : to provide the...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.