我有類如下。我封裝了矢量來擺脫循環。工作實例可以在link被發現(感謝在評論部分,以幫助)爲原始指針,智能指針和值編譯時間變化的成員函數
template<class T, class U>
std::shared_ptr<T> Cast(const std::shared_ptr<U>& spObject) // rename from CastTerrainObject
{
return std::dynamic_pointer_cast<T>(spObject);
}
template<class T, class U>
T Cast(U* r)
{
return dynamic_cast<T>(r);
}
template<typename value>
struct A
{
template < typename T, typename F >
T Find(F filterFunction)
{
return DoFind<T>(filterFunction, std::is_pointer<T>());
}
template < typename T, typename F >
T DoFind(F filterFunction, std::true_type)
{
for (size_t i = 0; i < iteratableList.size(); i++)
{
auto castedTerrain = Cast<T>(iteratableList[i]);
if (castedTerrain && filterFunction(castedTerrain))
return iteratableList[i];
}
return T();
}
template < typename T, typename F >
T DoFind(F filterFunction, std::false_type)
{
for (size_t i = 0; i < iteratableList.size(); i++)
{
auto castedTerrain = Cast<typename T::element_type>(iteratableList[i]);
if (castedTerrain && filterFunction(castedTerrain))
return iteratableList[i];
}
return T();
}
std::vector<value> iteratableList;
}
我班正在良好的智能指針
std::vector<std::shared_ptr<std::string>> names = { make_shared("needle"), make_shared("manyOtherNames") } ;
A<std::shared_ptr<std::string>> iterateable{ names };
iterateable.Find<std::shared_ptr<std::string>>([] (std::shared_ptr<std::string>){ return *in == "needle";});
和原始指針
std::vector<std::string*> names = { new std::string("needle"), new std::string("manyOtherNames") } ;
A<std::string*> iterateable{ names };
iterateable.Find<std::string*>([] (std::string* in){ return *in == "needle";});
但現在我想要使用它的值以及像(std :: string只是一個例子,我希望我的類可以使用任何非指針和非智能指針類型):
std::vector<std::string> names = { "needle", "manyOtherNames" } ;
A<std::string> iterateable{ names };
iterateable.Find<std::string>([] (std::string in){ return in == "needle";});
我還需要一個DoFind,但我無法看到我的案例的類型特徵。 如何組織我的代碼以實現我需要的功能。我限制在Visual Studio 2010中
你可以添加所有的用例(指針,智能指針,價值型)的例子嗎?我真的不明白你想要做什麼 –
希望現在看起來更好。 –
不,你的代碼不起作用:http://melpon.org/wandbox/permlink/WRKPcDp5nwcRyc0k –