1
我正在構建一個算法宿主類,並將數據存儲和一堆算法用作策略類。操作的數據在策略主機類中使用組合(Collection)進行封裝,兩種算法(第一,第二)公開繼承(多重繼承)。重載策略類模板的成員函數
當我嘗試從主機類的成員函數訪問策略類模板的成員函數時,我只能使用完全限定名稱來執行此操作,並且我期望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。謝謝!
項目33,Effective C++。 (headbang)謝謝! :) – tmaric 2013-04-08 17:02:31