Overloading variadic templated methods

  c++, c++17, overloading, variadic-templates

My compiler is having a hard time to understand this code, I am strugling for hours to find what’s wrong.

template<typename Derived>
struct AssetLoader {
    template<typename... Args>
    void doLoad(Args&& ... args) const {
        static_cast<const Derived *>(this)->load(std::forward<Args>(args)...);
    }
};

struct TextureLoader : public AssetLoader<TextureLoader> {
    void load(const std::string &path) const {
        // some code
    }
};

template<typename DefaultLoader>
class Resources {
    AssetLoader<DefaultLoader> m_defaultLoader;

public:
    Resources(AssetLoader<DefaultLoader> defaultLoader):
        m_defaultLoader(std::move(defaultLoader)) {}

    template<typename... Args>
    void load(Args&& ... args) {
        load(m_defaultLoader, std::forward<Args>(args)...);
    }

    template<typename Loader, typename... Args>
    void load(const AssetLoader<Loader>& loader, Args&& ... args) {
        loader.doLoad(std::forward<Args>(args)...);
    }
};

int main() {
    Resources<TextureLoader> resources(TextureLoader{});
    resources.load("image.png");
    resources.load(SomeOtherLoader{}, "example.jpg");
}

I got this error:

fatal error: template instantiation depth exceeds maximum of 900 (use -ftemplate-depth= to increase the maximum)
         return load(m_defaultLoader, std::forward<Args>(args)...);
                                      ~~~~~~~~~~~~~~~~~~^~~~~~

My real code is a lot more complex but I trimed it down to this, but I got the same error.

If I comment the first overload, it works well but I can’t call the load() method without passing a Loader. I want the overload for the default loader, so I can do resources.load("image.png");

I use mingw64 8.1

Any idea ?

Thank you !

Source: Windows Questions C++

LEAVE A COMMENT