473,799 Members | 3,442 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is partial deduction of generic parameters possible

Vin
Hi,

I just wanted to check if I am missing something or if what I am
looking for isn't possible.

I have a function with 2 generic parameter types, one of these occurs
in the argument list the other doesn't.

So the function is like this:
void CreateEntities< T, IT>(IList<ITent ities){}
I'd like to be able to call it as follows:
IList<IMyTypeen tities = new List<IMyType>() ;
CreateEntities< MyType>(entitie s);

So 'IT' would be deduced as 'IMyType' and 'T' is explicitly set to
'MyType'.

The reasoning behind this is that in the function I will do the
following
entities.Add(ne w T());
I haven't been able to get this syntax to work.

I have to have:
CreateEntities< MyType, IMyType>(entiti es);
If I change the function to take an extra useless parameter of type
'MyType' it'll deduce the arguments.
So this will work:
void CreateEntities< T, IT>(IList<ITent ities, T t){}
CreateEntities( entities, new MyType());
but clearly that isn't a useful option.
So is it possible for the compiler to deduce one parameter type or is
it all or none?

Thanks, Vin
Jun 27 '08 #1
10 2180
On May 14, 3:40 pm, Vin <vfinn...@gmail .comwrote:

<snip>
So is it possible for the compiler to deduce one parameter type or is
it all or none?
All or none, I'm afraid.

Jon
Jun 27 '08 #2
Vin
On May 14, 3:44*pm, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
All or none, I'm afraid.
Thought it might be :o(

Thanks, Vin
Jun 27 '08 #3
A shame really... it would be nice (especially when working with
anonymous types) to be able to mix'n'match - perhaps something like:

SomeFunc<?,int> (...)

(where ? means "infer")

I can't think of the examples right now, but I know I've had to refactor
things a few times to get around this... mainly where there isn't any
additional argument relating to a specific argument, or where the
compiler can't fully do the job by itself. Of course, if you have an
anonymous type it becomes very hard to help it!

(we all have our C# wishlists, this is on mine...)

Marc
Jun 27 '08 #4
On May 16, 3:48*pm, Marc Gravell <marc.grav...@g mail.comwrote:
A shame really... it would be nice (especially when working with
anonymous types) to be able to mix'n'match - perhaps something like:

SomeFunc<?,int> (...)

(where ? means "infer")

I can't think of the examples right now, but I know I've had to refactor
things a few times to get around this... mainly where there isn't any
additional argument relating to a specific argument, or where the
compiler can't fully do the job by itself. Of course, if you have an
anonymous type it becomes very hard to help it!

(we all have our C# wishlists, this is on mine...)
It's been mentioned by other people, too:

http://www.interact-sw.co.uk/iangblo...mbda-inference

In particular, Ian mentions "in C# team member blogs" which sounds
positive.
Not that I'd like to guess whether it'll be in C# 4 or not...

Jon

Jun 27 '08 #5
It's been mentioned by other people, too

Good link, cheers. I like the terminology: "mumble types" ;-p

Marc
Jun 27 '08 #6
Vin
On May 16, 4:25*pm, "Jon Skeet [C# MVP]" <sk...@pobox.co mwrote:
On May 16, 3:48*pm, Marc Gravell <marc.grav...@g mail.comwrote:
A shame really... it would be nice (especially when working with
anonymous types) to be able to mix'n'match - perhaps something like:
SomeFunc<?,int> (...)
(where ? means "infer")
I can't think of the examples right now, but I know I've had to refactor
things a few times to get around this... mainly where there isn't any
additional argument relating to a specific argument, or where the
compiler can't fully do the job by itself. Of course, if you have an
anonymous type it becomes very hard to help it!
(we all have our C# wishlists, this is on mine...)

It's been mentioned by other people, too:

http://www.interact-sw.co.uk/iangblo...mbda-inference

In particular, Ian mentions "in C# team member blogs" which sounds
positive.
Not that I'd like to guess whether it'll be in C# 4 or not...

Jon
Hi,

Maybe I am missing something but why would you need the extra syntax?

I'd have thought anything not available from arguments would have to
be specified and anything available wouldn't so the current syntax
would work fine.

void Fn<T1, T2, T3>(T2 t2, T3 t3) {}
would have to be called as
Fn<int>(0, 8);

This does put the restriction that explicit types would have to come
first but the call is cleaner than having to call
Fn<int, ?, ?>(0, 8);

I know this is all speculation at this stage but is there an advantage
to the mumble syntax?

Vin
Jun 27 '08 #7
On May 21, 11:11 am, Vin <vfinn...@gmail .comwrote:
Maybe I am missing something but why would you need the extra syntax?

I'd have thought anything not available from arguments would have to
be specified and anything available wouldn't so the current syntax
would work fine.
I suspect you'd find it would work fine in some simple cases but not
other more complicated ones. For instance:
void Fn<T1, T2, T3>(T2 t2, T3 t3) {}
would have to be called as
Fn<int>(0, 8);
And what about when I want to pass null as the second argument? It
would be nice to be able to specify the type for that without casting
the null.

There are various other times where you want to explicitly state the
area of ambiguity.
This does put the restriction that explicit types would have to come
first but the call is cleaner than having to call
Fn<int, ?, ?>(0, 8);
That's true when it's blindingly obvious which types you want to be
inferred. Other times it may not be so obvious, and that's where
mumble typing helps.

Jon
Jun 27 '08 #8
void Fn<T1, T2, T3>(T2 t2, T3 t3) {}
would have to be called as
Fn<int>(0, 8);
And what if we also have a Fn<T>(...)?

Marc
Jun 27 '08 #9
On May 21, 1:16 pm, Marc Gravell <marc.grav...@g mail.comwrote:
void Fn<T1, T2, T3>(T2 t2, T3 t3) {}
would have to be called as
Fn<int>(0, 8);

And what if we also have a Fn<T>(...)?
That's a much better answer than mine :)

Jon
Jun 27 '08 #10

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

Similar topics

14
3920
by: Gianni Mariani | last post by:
Does anyone know if this is supposed to work ? template <unsigned N> int strn( const char str ) { return N; } #include <iostream>
14
2907
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
2285
by: Erik Wikström | last post by:
In school (no I will not ask you to do my schoolwork for me) we talked about policy-based design and got an assignment where we got the a code- fragment from a stack-implementation. The idea with the code was that if, when its destructor was called, it still contained any elements it would run 'delete' on them if they were pointers and do nothing if they were not. Our assignment was to reimplement the stack using policies so that the...
4
2568
by: Neelesh | last post by:
Hi all, I had some confusion about deduction of non-type template parameters for function templates : template <class T, int i> void foo (T, int p = i) ; void bar() { foo<int, 10>(40,40); // OK, T = int, i = 10 foo(40,40); // ERROR, T = int but i cannot be deduced.
5
2038
by: Andreas Mueller | last post by:
Hi All, I have an overloaded generic method anmed "Foo": Class Test Class Xox(Of T) End Class Class Lulli(Of T) Inherits Xox(Of T) End Class
6
2811
by: rincewind | last post by:
Hi, can anybody summarise all options for partial template specialization, for all kind of parameters (type, nontype, template)? I *think* I understand options for partial specialization on type parameters - in place of a template argument one can construct arbitrary valid C++ type declaration, more or less like in "typedef" statement. What about nontype parameters? Am I right that you cannot partially
2
2376
by: coolpint | last post by:
Can anyone kindly provide an explanation as to why the compiler does not "see" the function template in the contrieved code below? I think the argument deduction fails but can't figure out exactly what the problem is. Is it related to the fact that a nested class is involved? #include <iostream> using std::cout;
6
2903
by: year1943 | last post by:
For template <typename Tclass My ; I can define partial spec-ns somewhat like template <typename Tclass My<T*; or template <typename Tclass My<Another<T ; And full spec-n, say template <class My<int; What if I have a template class template <typename T, typename Uclass My ;
4
2546
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
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10490
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10259
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10030
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6809
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5589
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4145
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.