在 lambda 表达式和模板 typedef 习惯用法中提取对成员
我这里有一些复杂的类型,所以我决定使用漂亮的技巧在模板化类型上使用 typedef。然后我有一个类 some_container ,它有一个容器作为成员。容器是由元素和向量组成的对的向量。我想用 lambda 表达式编写 std::find_if 算法来查找具有特定值的元素。为了获取值,我必须首先调用pair,然后从元素中获取值。在我的 std::find_if 下面有一个正常的循环可以解决这个问题。我的 lambda 无法编译。如何访问对内元素内的值? 我使用 g++ 4.4+ 和 VS 2010,现在我想坚持使用 boost lambda。
#include <vector>
#include <algorithm>
#include <boost\lambda\lambda.hpp>
#include <boost\lambda\bind.hpp>
template<typename T>
class element
{
public:
T value;
};
template<typename T>
class element_vector_pair // idiom to have templated typedef
{
public:
typedef std::pair<element<T>, std::vector<T> > type;
};
template<typename T>
class vector_containter // idiom to have templated typedef
{
public:
typedef std::vector<typename element_vector_pair<T>::type > type;
};
template<typename T>
bool operator==(const typename element_vector_pair<T>::type & lhs, const typename element_vector_pair<T>::type & rhs)
{
return lhs.first.value == rhs.first.value;
}
template<typename T>
class some_container
{
public:
element<T> get_element(const T& value) const
{
std::find_if(container.begin(), container.end(), bind(&typename vector_containter<T>::type::value_type::first::value, boost::lambda::_1) == value);
/*for(size_t i = 0; i < container.size(); ++i)
{
if(container.at(i).first.value == value)
{
return container.at(i);
}
}*/
return element<T>(); //whatever
}
protected:
typename vector_containter<T>::type container;
};
int main()
{
some_container<int> s;
s.get_element(5);
return 0;
}
I have some complex types here so I decided to use nifty trick to have typedef on templated types. Then I have a class some_container that has a container as a member. Container is a vector of pairs composed of element and vector. I want to write std::find_if algorithm with lambda expression to find element that have certain value. To get the value I have to call first on pair and then get value from element. Below my std::find_if there is normal loop that does the trick. My lambda fails to compile. How to access value inside element which is inside pair?
I use g++ 4.4+ and VS 2010 and I want to stick to boost lambda for now.
#include <vector>
#include <algorithm>
#include <boost\lambda\lambda.hpp>
#include <boost\lambda\bind.hpp>
template<typename T>
class element
{
public:
T value;
};
template<typename T>
class element_vector_pair // idiom to have templated typedef
{
public:
typedef std::pair<element<T>, std::vector<T> > type;
};
template<typename T>
class vector_containter // idiom to have templated typedef
{
public:
typedef std::vector<typename element_vector_pair<T>::type > type;
};
template<typename T>
bool operator==(const typename element_vector_pair<T>::type & lhs, const typename element_vector_pair<T>::type & rhs)
{
return lhs.first.value == rhs.first.value;
}
template<typename T>
class some_container
{
public:
element<T> get_element(const T& value) const
{
std::find_if(container.begin(), container.end(), bind(&typename vector_containter<T>::type::value_type::first::value, boost::lambda::_1) == value);
/*for(size_t i = 0; i < container.size(); ++i)
{
if(container.at(i).first.value == value)
{
return container.at(i);
}
}*/
return element<T>(); //whatever
}
protected:
typename vector_containter<T>::type container;
};
int main()
{
some_container<int> s;
s.get_element(5);
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
两个问题。
您要追求的东西(
element::value
)不是类型名称。但是,首先您需要嵌套绑定:一个用于访问
_1.first
,另一个用于访问前一个的value
。没有 typedef:
所以,用你的习惯用法,没有不必要的 typename:
不过,这似乎不是特别可读。也许 a) 等待 C++0x lambda,b) 为
find_if
调用编写一个特定的函子,c) 只需执行手动循环。Two issues.
The thing that you are going after (
element<T>::value
) is not a typename.However, firstly you'll need nested binds: one to access
_1.first
and another one to access thevalue
of the previous.Without the typedefs:
And so, with your idiom, without the unnecessary typename:
This doesn't seem particularly readable, though. Perhaps a) wait for C++0x lambda, b) write a specific functor for the
find_if
call, c) just do a manual loop.