重载策略类模板的成员函数
Overloading member functions of policy class 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
中,它将再次找到基类的成员。这是另一个关于阴影的问题/答案。
相关文章:
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 将公共但非静态的成员函数与ALGLIB集成
- 使用指向成员的指针将成员函数作为参数传递
- 将重载的成员函数传递给函数模板
- 我不小心调用了一个没有自己类对象的成员函数.但这是怎么回事呢
- 如何在C++中使用非静态成员函数作为回调函数
- C++错误C2600:无法定义编译器生成的特殊成员函数(必须首先在类中声明)
- 关联容器的下界复杂性:成员函数与非成员函数
- 在 C++ 中用派生类型重写成员函数
- 链表的泛型函数remove()与成员函数remove)
- 如何将lambda作为模板类的成员函数参数
- constexpr构造函数需要常量成员函数时出现问题
- 将自由函数绑定为类成员函数
- 区分非成员函数和头文件中的成员函数
- 如何从子成员函数修改父公共成员变量
- 保留对其他类的成员函数的引用
- 在运算符重载定义中使用成员函数(const错误)
- 内联如何影响模块接口中的成员函数
- 将成员函数指针作为参数传递给模板方法