BOOST_FOREACH缺陷

本文探讨了在C++中使用BOOST_FOREACH宏时遇到的问题,特别是针对std::map的迭代。介绍了如何通过预定义循环变量或使用typedef来解决模板参数过多的问题,并展示了通过不同方式定义循环变量对迭代的影响。

以下来自手册:

缺陷

本节描述 BOOST_FOREACH 中的一些常见的缺陷。

带逗号的类型

由于 BOOST_FOREACH 是一个宏,它必须刚好有两个参数,并以一个逗号分隔它们。这并不总是很方便,尤其当循环变量的类型是一个模板的时候。考虑一下对 std::map 进行迭代:

std::map<int,int> m;

// 错误!BOOST_FOREACH 宏参数过多。
BOOST_FOREACH(std::pair<int,int> p, m) // ...

一个解决方法是使用 typedef.

std::map<int,int> m;
typedef std::pair<int,int> pair_t;

BOOST_FOREACH(pair_t p, m) // ...

另一个解决方法是预先声明循环变量:

std::map<int,int> m;
std::pair<int,int> p;

BOOST_FOREACH(p, m) // ...

测试代码:

// boost_foreach_map.cpp : 定义控制台应用程序的入口点。
// BOOST_FOREACH缺陷

#include "stdafx.h"
#include <map>
#include <iostream>
#include <string>
#include <utility>
#include <vector>
#include <list>
#include <boost/foreach.hpp> 
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
	map<int,string> map_list; 
	//map_list: (1,"11") (2,"22") (3,"33");
 	
// 	map_list.insert(Pair(1, "11"));
// 	map_list.insert(Pair(2, "22"));
// 	map_list.insert(Pair(3, "33"));
	map_list.insert(make_pair(1, "11"));
	map_list.insert(make_pair(2, "22"));
	map_list.insert(make_pair(3, "33"));
// 	map_list[1]="11";
// 	map_list[2]="22";
// 	map_list[3]="33";

	// 不能放到BOOST_FOREACH括号中,模板参数过多,无法推导
// 	BOOST_FOREACH(const pair<int, string>& p, map_list) // 在内部定义,有错
// 	BOOST_FOREACH(const map<int, string>::value_type& p,  map_list) // 有错

//	map<int, string>::value_type p;  //在BOOST_FOREACH中使用,有错 
//	map<const int, string>::value_type p;  // 有错 
//	const map<int, string>::value_type p; // 有错 
	
// 	pair<const int, string> p; // 有错, 奇怪的是,若使用typedef,正确
// 	const pair<int, string> p; // 有错 
// 	const pair<const int, string> p; // 有错 
	pair<int, string> p; // 在外部定义,只有这种方式才正确
	BOOST_FOREACH(p, map_list)
	{
		cout<<p.first<<" "<<p.second<<endl;
	}
	
//	typedef pair<int,string> Pair; //  正确
	typedef pair<const int,string> Pair;
	// 没有办法使用BOOST_FOREACH改变map的值,没办法定义非const引用
 	BOOST_FOREACH(const Pair& p, map_list) // 使用typedef,(Pair p, map_list):正确 
 	{
 		cout<<p.first<<" "<<p.second<<endl;
 	} 

	// vector:无此缺陷
	vector<int> vec(10,9);
	BOOST_FOREACH(int i, vec)
	{
		i = 10; // 改变不作用于vector
	}
	BOOST_FOREACH(const int& i, vec)
	{
		cout<<i<<endl; // 9
	}
	BOOST_FOREACH(int& i, vec)
	{
		i = 10; // 改变作用于vector
		cout<<i<<endl;
	}
	BOOST_FOREACH(const int i, vec)
	{
		cout<<i<<endl; // 10
	}

	return 0;
}




/work/pcl-cross-compile/src/boost$ ls /work/pcl-cross-compile/install/boost/include/ algorithm dynamic_bitset.hpp make_unique.hpp qvm throw_exception.hpp align enable_shared_from_this.hpp math qvm.hpp timer aligned_storage.hpp exception math_fwd.hpp qvm_lite.hpp timer.hpp align.hpp exception_ptr.hpp move random token_functions.hpp archive foreach_fwd.hpp mpi random.hpp token_iterator.hpp asio foreach.hpp mpi.hpp range tokenizer.hpp asio.hpp format mpl range.hpp tti assign format.hpp multi_array rational.hpp tuple assign.hpp functional multi_array.hpp redis type_erasure call_traits.hpp functional.hpp multi_index redis.hpp type_index cerrno.hpp fusion multi_index_container_fwd.hpp regex type_index.hpp charconv geometry multi_index_container.hpp regex_fwd.hpp typeof charconv.hpp geometry.hpp multiprecision regex.h type_traits circular_buffer gil nondet_random.hpp regex.hpp type_traits.hpp circular_buffer_fwd.hpp gil.hpp none.hpp safe_numerics units circular_buffer.hpp graph none_t.hpp scoped_array.hpp unordered cobalt heap nowide scoped_ptr.hpp unordered_map.hpp cobalt.hpp histogram numeric scope_exit.hpp unordered_set.hpp compressed_pair.hpp histogram.hpp operators.hpp serialization url concept hof operators_v1.hpp shared_array.hpp url.hpp concept_archetype.hpp hof.hpp optional shared_ptr.hpp utility concept_check icl optional.hpp signals2 utility.hpp concept_check.hpp interprocess parameter signals2.hpp uuid config intrusive pending smart_ptr uuid.hpp config.hpp intrusive_ptr.hpp phoenix smart_ptr.hpp variant container io phoenix.hpp sort variant2 context io_fwd.hpp pointer_cast.hpp spirit variant2.hpp convert json pointer_to_other.hpp spirit.hpp variant.hpp convert.hpp json.hpp polygon stacktrace version.hpp cregex.hpp leaf pool stacktrace.hpp vmd cstdfloat.hpp leaf.hpp predef statechart wave cstdint.hpp limits.hpp predef.h static_assert.hpp wave.hpp cxx11_char_types.hpp locale program_options static_string weak_ptr.hpp date_time locale.hpp program_options.hpp static_string.hpp winapi date_time.hpp local_function progress.hpp stl_interfaces xpressive describe local_function.hpp property_map system yap describe.hpp lockfree proto system.hpp detail logic ptr_container test dynamic_bitset make_default.hpp python thread dynamic_bitset_fwd.hpp make_shared.hpp python.hpp thread.hpp uidq8326@hzh27145u:/work/pcl-cross-compile/src/boost$ ./b2 install toolset=gcc architecture=arm address-model=64 \--prefix=/work/pcl-cross-compile/install/boost -j$(nproc) Performing configuration checks - default address-model : 64-bit [1] - default architecture : arm [1] - symlinks supported : yes error: Unable to find file or target named error: '/boost/headers' error: referred to from project at error: 'libs/date_time/build'
最新发布
08-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值