1

I am building an algorithm host class and I'm using the data storage and a bunch of algorithms as policy classes. The data operated on is encapsulated using composition (Collection) within the policy host class, and two algorithms (First, Second) are inherited publicly (multiple inheritance).

When I try to access the member functions of policy class templates from member functions of the host class, I can only do so using the fully qualified name, and I expected that the ADL should be working from within the host class.

Here is the example code:

#include <iostream>


template<typename Element>
class MapCollection
{
    // Map implementation 
    public:

        // Programming to an (implicit) interface
        template<typename Key> 
        void insertElement(Element const & e, Key const& k) {}

        template<typename Key>
        void getElement(Element const& e, Key const& k) {}
};

template<typename Element>
class VectorCollection
{
    // Vector implementation
    public: 

        template<typename Key> 
        void insertElement(Element const & e, Key const& k) {}

        template<typename Key>
        void getElement(Element const& e, Key const& k) {}
};

template<typename Collection>
class FirstAlgorithm 
{
    public: 

        void firstExecute(Collection& coll)
        {
            std::cout << "FirstAlgorithm::execute" << std::endl;
        }
};

template<typename Collection>
class SecondAlgorithm
{
    public: 

        void secondExecute(Collection const & coll)
        {
            std::cout << "SecondAlgorithm::execute" << std::endl;
        }
};

template
<
    typename HostConfigurationTraits
>
class AlgorithmHost
:
    public HostConfigurationTraits::First,
    public HostConfigurationTraits::Second
{
    public:
        typedef typename HostConfigurationTraits::Collection Collection;

    private:
        Collection data_; 

    public: 

        // ADL not working?
        void firstExecute()
        {
            // This works: 
             //HostConfigurationTraits::First::firstExecute(data_);

            // This fails:
            this->firstExecute(data_);
        } 

        // ADL not working?
        void secondExecute()
        {
            // This works:
            //HostConfigurationTraits::Second::secondExecute(data_);

            // This fails:
            this->secondExecute(data_);
        }
};

class EfficientElement {};

struct DefaultAlgorithmHostTraits 
{
    typedef EfficientElement Element;
    typedef VectorCollection<Element> Collection;
    typedef FirstAlgorithm<Collection> First;
    typedef SecondAlgorithm<Collection> Second;
};

int main(int argc, const char *argv[])
{

    AlgorithmHost<DefaultAlgorithmHostTraits> host; 

    // Breaks here:
    host.secondExecute(); 
    host.firstExecute(); 

    return 0;
}

Is this caused by ADL, or did I misplace my doubts? :)

I'm using g++ 4.4.3. Thanks!

tmaric
  • 5,347
  • 4
  • 42
  • 75

1 Answers1

2

You case has nothing to do with ADL, but with the fact that your definition of firstExecute shadows the definition from the base class. If you add these lines:

using HostConfigurationTraits::First::firstExecute;
using HostConfigurationTraits::Second::secondExecute;

in AlgorithmHost, it will find the base class' members again. Here's another question/answer about shadowing.

Community
  • 1
  • 1
Daniel Frey
  • 55,810
  • 13
  • 122
  • 180