c++ - 重载策略类​​模板的成员函数

标签 c++ templates

我正在构建一个算法主机类,我正在使用数据存储和一堆算法作为策略类。操作的数据在策略宿主类内部使用组合(Collection)进行封装,公开继承两种算法(First,Second)(多重继承)。

当我尝试从宿主类的成员函数访问策略类模板的成员函数时,我只能使用完全限定名称这样做,并且我希望 ADL 应该在宿主类中工作。

示例代码如下:

#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;
}

这是ADL引起的,还是我的疑虑放错了地方? :)

我正在使用 g++ 4.4.3。谢谢!

最佳答案

您的案例与 ADL 无关,但与您对 firstExecute 的定义隐藏了基类的定义这一事实有关。如果添加这些行:

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

AlgorithmHost中,它会再次找到基类的成员。这是另一个 question/answer关于阴影。

关于c++ - 重载策略类​​模板的成员函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15884398/

相关文章:

c++ - 模板类定义中的模板方法与声明不匹配

python - Django : How to structure template folder considering template tag includes?

C++ constexpr : Compute a std array at compile time

c++ - 我是否需要一个 extern "C" block 来包含标准 POSIX C header ?

c++ - 函数模板

c++ - 清除任意二维数组

C++ 函数模板格式化

visual-studio - MSVS : How can I set the x64 as the default, 而不是 AnyCPU?

c++ - C++ 中的通用 Functor 类

c++ - 数组的加减法