Static and non-static method overloading

Gor Gyolchanyan gor.f.gyolchanyan at gmail.com
Tue Sep 27 08:11:32 PDT 2011


I have a question about static and non-static methods overloading.
I have this module:

--------------------------------------------------------------------------------

/**
  * Dynamic library interfacing module.
  */
module dynlib;

public:
    /**
      * A lazily loaded cached dynamic library.
      */
    class DynLib
    {
    public:
        alias void* Symbol;

        /**
          * Loads or returns already loaded dynamic library.
          */
        static DynLib opIndex(in char[] name)
        {
            auto lib = name in _libraries;
            if(lib)
                return *lib;
            _libraries[name] = new DynLib(name);
            return DynLib[name];
        }

        /**
          * Loads or returns already loaded symbol from this dynamic
library.
          */
        Symbol opIndex(in char[] name)
        {
            auto sym = name in _symbols;
            if(sym)
                return *sym;
            version(Windows)
                _symbols[name] = GetProcAddress(_handle, name.toStringz);
            else version(Posix)
                _symbols[name] = dlsym(_handle, name.toStringz);
            return DynLib[name];
        }

        bool empty() @property
        {
            return _handle is null;
        }

    private:
        alias void* Handle;

        static DynLib[string] _libraries;
        static Symbol[string] _symbols;
        Handle _handle;

        this(in char[] name)
        {
            version(Windows)
                _handle = LoadLibraryA(name.toStringz);
            else version(Posix)
                _handle = dlopen(name.toStringz, RTLD_NOW);
        }

        ~this()
        {
            version(Windows)
                FreeLibrary(_handle);
            version(Posix)
                dlclose(_handle);
        }

        unittest
        {
            DynLib dl;
            version(Windows)
                dl = DynLib["OpenGL32.dll"];
            version(Posix)
                dl = DynLib["libGL.so"];
            assert(!dl.empty);

            DynLib.Symbol sym = dl["glClearColor"];
            assert(sym !is null);
        }
    }

private:
    import std.string: toStringz;

    version(Windows)
        import core.sys.windows.windows: LoadLibraryA, FreeLibrary,
GetProcAddress;
    else version(Posix)
        import core.sys.posix.dlfcn: dlopen, dlclose, dlsym;

--------------------------------------------------------------------------------

And when i compile this, i get an unexpected error:

--------------------------------------------------------------------------------

dynlib.d(24): Error: function dynlib.DynLib.opIndex called with argument
types:
        ((const(char[])))
matches both:
        dynlib.DynLib.opIndex(in const(char[]) name)
and:
        dynlib.DynLib.opIndex(in const(char[]) name)
dynlib.d(39): Error: function dynlib.DynLib.opIndex called with argument
types:
        ((const(char[])))
matches both:
        dynlib.DynLib.opIndex(in const(char[]) name)
and:
        dynlib.DynLib.opIndex(in const(char[]) name)
dynlib.d(39): Error: cannot implicitly convert expression (opIndex(name)) of
type dynlib.DynLib to void*

--------------------------------------------------------------------------------

My point is: How can this be ambiguous, when i explicitly call the static
method from the class name, not the object and when i call a method from an
object, the most obvious choice is the non-static one?

Cheers,
Gor.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.puremagic.com/pipermail/digitalmars-d/attachments/20110927/972b0c16/attachment-0001.html>


More information about the Digitalmars-d mailing list