Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make Eclipse CDT auto-indent properly when using BOOST_FOREACH?

I write this tiny C++ example in Eclipse 3.4.1 (CDT 5.0.1):

#include <iostream>
#include <vector>
#include <boost/foreach.hpp>

int foo()
{
  std::vector<int> numbers;
  BOOST_FOREACH(int n, numbers)
  {
    std::cout << n << std::endl;
  }
  std::cout << numbers.size << std::endl;
}

Then I hit Shift+Ctrl+F to format my code, and it becomes:

#include <iostream>
#include <vector>
#include <boost/foreach.hpp>

int foo()
{
    std::vector<int> numbers;
    BOOST_FOREACH(int n, numbers)
{   std::cout << n << std::endl;
}
std::cout << numbers.size << std::endl;
}

This is with the BSD/Allman Code Style. Other styles obviously vary the look of the formatted code, but none give correct indentation.

When I use the format feature on a larger piece of code, subsequent functions or methods are also affected by too little indentation, making the formatting help pretty unhelpful.

Is there something I can do to make the indentation work properly with BOOST_FOREACH?

like image 465
activout.se Avatar asked Dec 14 '08 10:12

activout.se


3 Answers

Add this to some header used by your code:

#ifdef __CDT_PARSER__
    #undef BOOST_FOREACH
    #define BOOST_FOREACH(a, b) for(a; ; )
#endif
like image 162
Pescuma Avatar answered Nov 02 '22 22:11

Pescuma


It is still broken in the current CDT for Kepler. But there is a bug in Eclipse's Bugzilla (check its status here). It will eventually be fixed :-)

like image 2
Haplo Avatar answered Nov 03 '22 00:11

Haplo


You might want to try the astyle eclipse plugin. It seems to be much nicer than the default eclipse style of C++ indentation.

like image 2
Nik Reiman Avatar answered Nov 03 '22 00:11

Nik Reiman