473,549 Members | 2,455 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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<typena me 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<typena me 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<typena me 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 2201
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*********@exc ite.com (C Johnson)
wrote:
So here it is as follows with the same questions as my original post:

#include <iostream>

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

template<typen ame 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*>(anythin g))
// 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********@hotm ail.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
4059
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 is used? ] Are template functions, objects and member functions then placed as separate entities within the object file, as with any non templated
3
1916
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
1713
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 discussion in the above book is too abstract to read.
14
2872
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&); template<typename T>
4
2506
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 "template bool space_pred(basic_string<T,char_traits<T>,allocator<T> >::value_type)". If I use gcc4.0..2, I get,
7
1521
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
969
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 code for me so I don't have to type all of it in. I'm sure this is possible. Apparently you can do it from the class designer somehow by openning...
4
2533
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, template <class T> void foo(T t) { ... }
0
1376
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 definition of the template at every place where it is : used. This means problems with include separation and problems with : code duplication (the...
0
7736
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. ...
0
7982
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...
1
7500
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...
0
6066
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5385
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5110
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3514
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3494
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
783
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...

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.