Should this program compile?
-
Friday, August 17, 2012 1:34 AM
int foo() restrict(amp) {
return 111;
}
int foo() restrict(cpu) {
return 999;
}
void* get_address() {
return static_cast<void*>(foo);
}Error 1 error C2440: 'static_cast' : cannot convert from 'overloaded-function' to 'void *'
All Replies
-
Friday, August 17, 2012 5:47 PMOwner
Hi wwwwpan,
Good question! First of all, you will get same error if you change the program to:
int foo(int)
{
return 111;
}int foo(float)
{
return 999;
}void * get_address()
{
return static_cast<void*>(foo);
}Simply provide the overloaded function name foo is not enough to disambiguate between the two foos in this case.
Now you could argue that since get_address is restrict(cpu), compiler could choose the restrict(cpu) version of foo to disambiguate. This could be one possible semantics. But we choose not to do so because if we use the ambient context to eliminate the restrict(amp) candidate for this case, then it's not clear what should be done if there is only one foo, which is restrict(amp). Error since there is no restrict(cpu) candidate but the ambient context is restrict(cpu), or bound to the restrict(amp) overload since there is no ambiguity? Note that we currently don't allow function pointers with restrict(amp) in v1, but we probably would relax that restriction in future. So we choose to make it ambiguous for this case. You could always use type cast to help the compiler to pick the one you want, like:
return static_cast<void*>((int (*)() restrict(cpu))foo);
"restrict(cpu)" could be omitted above since it's the default.
Does this make sense? Let me know if you have more questions.
Thanks,
Lingli- Proposed As Answer by Zhu, Weirong Friday, August 17, 2012 7:58 PM
-
Friday, August 17, 2012 6:57 PM
But we choose not to do so because if we use the ambient context to eliminate the restrict(amp) candidate for this case, then it's not clear what should be done if there is only one foo, which is restrict(amp).
An error is logical? Like inside a const member function you cannot call a non-const member function.
-
Friday, August 17, 2012 7:18 PM
Also the following code compiles
int foo() { return 111; } int foo() restrict(amp) { return 999; } int bar() restrict(amp, cpu) { return foo(); // (*) }however, which foo function is called in (*) is determined by the call instance to bar. I.e the overload resolution is not unique during the parsing of declaration bar. This seems odd in c++.
-
Friday, August 17, 2012 7:19 PMOwnerHere, you are not invoking the function, instead, you are taking the address of the function. You can taking the address of non-const member function inside a const member function, right?
-
Friday, August 17, 2012 7:25 PMOwner
Yeah, this is by-design. Please refer to Function overloading with restrict in C++ AMP for more information regarding this topic.
The difference between this example and the example in your original post is that one invokes the function, the other takes the address of the function. Currently, we do restrict context sensitive resolution for function invocations, but not for taking address of functions. We are actively discussing the semantic for v.next, so your feedbacks would be very appreciated.
-
Friday, August 17, 2012 7:32 PMGreat blog and thanks for answering my questions! It is interesting and deserves more thinking.
- Edited by wwwwpan Friday, August 17, 2012 7:32 PM
-
Friday, August 17, 2012 7:39 PM
This implies that the first will not compile but the second will.
int bar() restrict(amp, cpu) {
auto f = foo;
return f();
}int bar() restrict(amp, cpu) {
return foo();
} -
Friday, August 17, 2012 7:48 PMOwner
Yes. We want to differentiate function invocations from function address taking so that in future we could potentially taking address of an amp-restricted function inside a restrict(cpu) function if we start to support function pointer with restrict(amp). If you always eliminate overload candidates based on ambient context, you will not be able to do that.- Proposed As Answer by Zhu, Weirong Friday, August 17, 2012 7:58 PM
- Marked As Answer by wwwwpan Friday, August 17, 2012 8:01 PM

