C++ 重载策略类模板的成员函数
我正在构建一个算法宿主类,并将数据存储和一系列算法用作策略类。操作的数据在策略主机类中使用组合(集合)进行封装,两个算法(第一个、第二个)公开继承(多重继承) 当我试图从宿主类的成员函数访问策略类模板的成员函数时,我只能使用完全限定名来访问,我希望ADL应该在宿主类中工作 下面是示例代码:C++ 重载策略类模板的成员函数,c++,templates,C++,Templates,我正在构建一个算法宿主类,并将数据存储和一系列算法用作策略类。操作的数据在策略主机类中使用组合(集合)进行封装,两个算法(第一个、第二个)公开继承(多重继承) 当我试图从宿主类的成员函数访问策略类模板的成员函数时,我只能使用完全限定名来访问,我希望ADL应该在宿主类中工作 下面是示例代码: #include <iostream> template<typename Element> class MapCollection { // Map implementa
#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;
}
#包括
模板
类映射集合
{
//映射实现
公众:
//编程到(隐式)接口
模板
void insertElement(元素常量和e,键常量和k){}
模板
void getElement(元素常量和e,键常量和k){}
};
模板
类向量集合
{
//向量实现
公众:
模板
void insertElement(元素常量和e,键常量和k){}
模板
void getElement(元素常量和e,键常量和k){}
};
模板
类优先算法
{
公众:
void firstExecute(收集和收集)
{
std::cout您的案例与ADL无关,但是您对firstExecute
的定义与基类的定义存在阴影。如果添加以下行:
using HostConfigurationTraits::First::firstExecute;
using HostConfigurationTraits::Second::secondExecute;
>代码>算法宿主< /C>,它将再次找到基类的成员。这里是另一个关于阴影的问题。
项目33,有效C++。(头版)谢谢!