|
Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript |
D - delegate and function pointer overloading
Hi,
The DMD compiler (0.50) doesn't complain when I overload a function to
accept both function pointers or delegates, as in:
bit all(char[] array, bit (*predicate)(char)) {
for (int i = 0; i < array.length; i++) {
if (!predicate(array[i])) {
return false;
}
}
return true;
}
bit all(char[] array, bit delegate(char) predicate) {
for (int i = 0; i < array.length; i++) {
if (!predicate(array[i])) {
return false;
}
}
return true;
}
bit isVowel(char c) {
return (c == 'a') || (c == 'e') || (c == 'i') || (c == 'o') || (c ==
'u');
}
class Character {
private char letter;
this(char c) {
this.letter = c;
}
public bit isLetter(char c) {
return this.letter == c;
}
}
int main() {
Character a = new Character('a');
bit delegate(char) isLetter;
isLetter = &a.isLetter;
// printf("%d\r\n", all("aeiouoeieuiei", &isVowel));
printf("%d\r\n", all("aeiouoeieuiei", isLetter));
return 0;
}
But if we uncomment the call to the overloaded all function using a
function pointer, the compiler says that both functions match for the
function pointer parameter. If I have just one version the code doesn't
compile, because it says it can't cast from fp to delegate and vice-versa.
Is it a bug or I'm missing something?
Best regards,
Daniel Yokomiso.
"this line has five words
and also five syllables
that line had seven"
- Liz Cordingley
Nov 20 2002
|