Solved

Question about BOOST_FOREACH

Posted on 2014-01-25
1
175 Views
Last Modified: 2014-09-30
Suppose I have a std::vector of double called myVec, and a std::map of type <int, double> called myMap. Why is it that the following works

BOOST_FOREACH(double x, myVec) x = 10.0;

Open in new window

and the following gives an error
BOOST_FOREACH(pair<int,double> y, myMap) 
cout << y.first << "\t" << y.second << endl;

Open in new window

so that I need to use

pair<int,double> y;
BOOST_FOREACH(y, myMap) cout << y.first << "\t" << y.second << endl;

Open in new window

instead?
0
Comment
Question by:Rothbard
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 3

Accepted Solution

by:
kuroji earned 200 total points
ID: 40088317
It's the comma in the expression
pair<int, double>
The C Preprocessor is not type aware.

Write it like this and you're good to go
typedef std::map<int, double>::value_type map_value_type;

BOOST_FOREACH(map_value_type y, myMap) 
    std::cout << y.first << "\t" << y.second << std::endl;

Open in new window

0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question