使用 std::tr1::bind 或 std::tr1::mem_fn 时,可以使用 std::not1 的替代方案
what alternative to std::not1 is to be used when using std::tr1::bind or std::tr1::mem_fn
我有一个我的类 Foo 的智能 ptr 向量:
struct Foo
{
Foo() : mEnabled( false ) {}
bool mEnabled;
bool isEnabled() const { return mEnabled; }
void setEnabled( bool inEnabled ) { mEnabled = inEnabled; }
/* ... */
};
typedef std::tr1::shared_ptr< Foo > tFooPtr;
typedef std::vector< tFooPtr > tFooVec;
我有这个工作得很好:
tFooVec foo_vector; // insert couple of elements
size_t count = count_if( foo_vector.begin(), foo_vector.end(), std::tr1::mem_fn( &Foo::isEnabled ) );
但是当我想count_if"禁用"Foo对象时,使用什么功能"助手"
size_t count = count_if( foo_vector.begin(), foo_vector.end(), std::not1( std::tr1::mem_fn( &Foo::isEnabled ) ) ); // does not compile
上面的行不编译:
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:446: error: no match for call to '(std::unary_negate<std::tr1::_Mem_fn<bool (Foo::*)()const> >) (std::tr1::shared_ptr<Foo>&)'
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_function.h:322: note: candidates are: bool std::unary_negate<_Predicate>::operator()(const typename _Predicate::argument_type&) const [with _Predicate = std::tr1::_Mem_fn<bool (Foo::*)()const>]
make: *** [src/shared_ptr_tests.o] Error 1
(在 Linux 上使用 g++ 4.1.2)
我认为编译问题来自std::not1
正在使用std::unary_negate
,这需要函数/谓词提供的Predicate::argument_type
。后者是在谓语派生自std::unary_function
叹息时给出
话虽如此,我假设std::tr1::mem_fn
没有使用std::unary_function
也没有提供argument_type
。
我现在使用的解决方案是,我现在使用 boost::bind 而不是 std::tr1::bind
#include <boost/bind.hpp>
using namespace boost;
...
size_t countboost = count_if( foo_vector.begin(), foo_vector.end(), !( bind( &Foo::isEnabled, _1 )) );
为了避免复杂化(和混淆),我在整个代码中将 std::tr1::bind 的用法替换为 boost::bind 。
!boost::bind(...为我工作:
bool test(int i)
{
return i < 2;
}
TEST( boost_bind_test, negateTest )
{
std::vector<int> vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
ASSERT_EQ(2, count_if( vec.begin(), vec.end(), !boost::bind(&test, _1)));
};
潜在的问题是mem_fun(isEnabled)
采用const Foo *
,而算法count_if
传递一个shared_ptr<Foo>
到它的谓词。
我不完全确定为什么这适用于mem_fn( &Foo::isEnabled )
,但不适用于not1(mem_fn( &Foo::isEnabled ))
.我所能想到的是,额外的包装器引入了对不同转换的需求。但是你可以解决它:
bool sharedEnabled(tFooPtr x) {
return x->isEnabled();
}
size_t count2 = std::count_if( foo_vector.begin(), foo_vector.end(), std::not1( std::ptr_fun(&sharedEnabled ) ) );
相关文章:
- 如何修复以下错误"no match for call to '(std::tr1::shared_ptr<_iobuf*>) (FILE*&)'"
- std :: tr1 :: shared_ptr throw bad_alloc,也是一个好主意
- std::tr1::函数和它接受的模板值
- c value_type对于std :: tr1不起作用:std :: map中的元组
- 擦除 std::tr1::unordered_map 中的元素
- GHashTable vs std::tr1::unordered_map
- boost::function 和 std::tr1::function 之间是否有重要的区别需要了解
- std::tr1::函数为VS2008 SP1中的x64配置提供链接错误
- std::tr1::shared_ptr and dynamic_cast
- boost::hash/std::tr1::hash 不为复制的 const char* 返回相同的哈希
- 将 std::tr1 导入 std - 合法吗?它是否提高了便携性
- G++4.8 中对"std::tr1::__detail::__prime_list"的未定义引用
- 使用 std::tr1::bind 或 std::tr1::mem_fn 时,可以使用 std::not1 的替代方案
- visual c++ std::tr1::hash::operator() undefined?
- 处理std::tr1::shared_ptr向量的约定
- 使用boost序列化、std::tr1::unordered_map和自定义键的奇怪行为
- std::tr1::将各种类型的函数对象存储在公共容器中
- std::tr1::函数赋值和绑定
- 使用std::tr1::sharedptr作为引用计数的内部机制
- Boost::shared_ptr vs std::tr1::shared_ptr在多操作系统上的编译