Hi all,
I have a method where the compiler is unable to deduce the generic
arguments. Now I'm asking myself if this a bug, or if not, why it isn't
working. The method in question is Foo3:
using System;
using System.Collecti ons.Generic;
namespace ConsoleApplicat ion10
{
class Program
{
class A<T> { }
class B<T> { }
static A<T> Foo1<T, AA>(AA a, T t) where AA : A<T>
{
return default(A<T>);
}
static A<T> Foo2<T, AA>(AA a, B<T> t) where AA : A<T>
{
return default(A<T>);
}
static A<T> Foo3<T, AA>(AA a) where AA : A<T>
{
return default(A<T>);
}
static void Main(string[] args)
{
A<int> ai = new A<int>();
ai = Foo1(ai, 42);
ai = Foo2(ai, new B<int>());
// ... gives CS0411, can't deduce generic parameter. Why?
ai = Foo3(ai);
}
}
}
The compiler can deduce T for Foo1, as it is explicitly defined as a
parameter. It also can infer T & AA in Foo2, although T is only there
implicitly as an generic argument.
But if it can handle Foo2, why not Foo3?
TIA,
Andy
--
To email me directly, please remove the *NO*SPAM* parts below:
*NO*SPAM*xmen40 @*NO*SPAM*gmx.n et 4 3041
Andreas Mueller <me@privacy.net > wrote: I have a method where the compiler is unable to deduce the generic arguments. Now I'm asking myself if this a bug, or if not, why it isn't working. The method in question is Foo3:
<snip>
static A<T> Foo3<T, AA>(AA a) where AA : A<T> { return default(A<T>); }
Looking at section 27.6.4 of the draft ECMA spec (the numbering keeps
changing, unfortunately - "Inference of type arguments" is the sub-
clause name) gives *some* light.
Each argument/parameter is processed in turn. Fortunately, we've only
got one to deal with here - the parameter of "AA a" and the argument of
"ai" (with type A<int>).
Given that AA is a method type parameter, it *looks* like we should go
for the option which says that type inference succeeds for this
parameter, and the type of AA is A<int>.
Unfortunately, I don't see anything in the spec which says that
inference should then take the constraints into account and further
infer what it can (in this case that T is int).
In neither Foo1 nor Foo2 is the constraint needed to work out T, which
is why there isn't a problem. (In the case of Foo2, the second
parameter is of a constructed type, and the last option in the spec
occurs, inferring T.)
I don't know whether this is the answer you were hoping for, and I'm
not *100%* sure it's right - I've been reading the spec quite a lot
recently, so I'm reasonably "in tune" with it, but there are some
tricky bits like this...
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Jon Skeet [C# MVP] wrote: Andreas Mueller <me@privacy.net > wrote:
I have a method where the compiler is unable to deduce the generic arguments. Now I'm asking myself if this a bug, or if not, why it isn't working. The method in question is Foo3:
<snip>
static A<T> Foo3<T, AA>(AA a) where AA : A<T> { return default(A<T>); }
Looking at section 27.6.4 of the draft ECMA spec (the numbering keeps changing, unfortunately - "Inference of type arguments" is the sub- clause name) gives *some* light.
Each argument/parameter is processed in turn. Fortunately, we've only got one to deal with here - the parameter of "AA a" and the argument of "ai" (with type A<int>).
Given that AA is a method type parameter, it *looks* like we should go for the option which says that type inference succeeds for this parameter, and the type of AA is A<int>.
Unfortunately, I don't see anything in the spec which says that inference should then take the constraints into account and further infer what it can (in this case that T is int).
I think that is the point, it needs the constraint to resolve the
parameter. If it is not spec'd, it is not a bug, but a feature request. In neither Foo1 nor Foo2 is the constraint needed to work out T, which is why there isn't a problem. (In the case of Foo2, the second parameter is of a constructed type, and the last option in the spec occurs, inferring T.)
I don't know whether this is the answer you were hoping for, and I'm not *100%* sure it's right - I've been reading the spec quite a lot recently, so I'm reasonably "in tune" with it, but there are some tricky bits like this...
Hi Jon,
this answer really helped me! What do you think about it? Should it
work? (I'm a bit spoiled from C++ templates, so I'd say yes :-) ).I'll
reread the spec and may log a feature request at MS.
Cheers,
Andy
--
To email me directly, please remove the *NO*SPAM* parts below:
*NO*SPAM*xmen40 @*NO*SPAM*gmx.n et
Andreas Mueller <me@privacy.net > wrote: Unfortunately, I don't see anything in the spec which says that inference should then take the constraints into account and further infer what it can (in this case that T is int).
I think that is the point, it needs the constraint to resolve the parameter. If it is not spec'd, it is not a bug, but a feature request.
Yup. In neither Foo1 nor Foo2 is the constraint needed to work out T, which is why there isn't a problem. (In the case of Foo2, the second parameter is of a constructed type, and the last option in the spec occurs, inferring T.)
I don't know whether this is the answer you were hoping for, and I'm not *100%* sure it's right - I've been reading the spec quite a lot recently, so I'm reasonably "in tune" with it, but there are some tricky bits like this...
this answer really helped me! What do you think about it? Should it work? (I'm a bit spoiled from C++ templates, so I'd say yes :-) ).I'll reread the spec and may log a feature request at MS.
You certainly might as well. I've made a note for the specification
committee, but in more of a "note that this means" rather than "this
should be fixed" way - MS is probably a better avenue for that.
--
Jon Skeet - <sk***@pobox.co m> http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Please do submit a feature request. I've wished for the same thing.
Jesse This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: SimonH |
last post by:
Hi all,
I would like to make a generic set of methods that could be called regardless
of the database behind the scenes.
One of the methods I would like would take a string sql statement and an
array of DataParameter objects.
The problem i have is there doesnt seem to be a generic DataParameter class
that I can instantiate. There only seems to be specific implementations like
|
by: Marcel Sebbao |
last post by:
I want to use a function object for various callbacks, and also as a
normal function.
But this function is a template. It is fine, but then I need to
specify all the time the template
parameter although it is always the same as the constructor argument:
e.g:
|
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.
|
by: yaniv.golan |
last post by:
Hi,
Either I am missing something, or there is a compiler bug in the way
inheritance from generic s is treated.
In the following code, class c2 inherits from c1_generic. c1_generic
has T as a generic paramter, and in the inheritance, class c2 specifies
c3_T as the value of this parameter.
class c1_generic defines 2 constructors, 1 of them taking 1 parameter.
|
by: Fei Liu |
last post by:
Hello,
We all know that a template function can automatically deduce its
parameter type and instantiate, e.g.
template <tpyename T>
void func(T a);
func(0.f);
This will cause func<floatto be instantiated. The user does not have
| |
by: Fei Liu |
last post by:
Hello,
We all know that a template function can automatically deduce its
parameter type and instantiate, e.g.
template <tpyename T>
void func(T a);
func(0.f);
This will cause func<floatto be instantiated. The user does not have
|
by: Tim Frink |
last post by:
Hi,
how can I set default values to template parameters?
I've this code:
class A
{
template<typename T>
void foo( string = "", T* = 0, bool (T::*func)(void) = 0 );
|
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)
{
...
}
|
by: Vin |
last post by:
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<ITentities){}
|
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...
|
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 most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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...
|
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 launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
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...
| |