Small opCall problem

bearophile bearophileHUGS at lycos.com
Sun May 30 08:04:31 PDT 2010


This is a small C++ program that compiles:

template<typename T> struct Foo {
    Foo(T x) {}
    template<typename U> void operator()(U y) {}
};
int main() {
    Foo<int> foo(1);
    foo(1.5);
}


I think this is an equivalent D2 program:

struct Foo(T) {
    this(T x) {}
    void opCall(U)(U y) {}
}
void main() {
    auto foo = Foo!int(1);
    foo(1.5);
}


But dmd 2.046 prints:
temp.d(7): Error: constructor temp.Foo!(int).Foo.this (int x) is not callable using argument types (double)
temp.d(7): Error: cannot implicitly convert expression (1.5) of type double to int

Is this a bug in my D code? If it's a bug in my D code, do you know how to translate that C++ code in D2?

Bye and thank you,
bearophile


More information about the Digitalmars-d-learn mailing list