Sunday, March 6, 2011

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?

From stackoverflow
  • Euh... you can't ? Try next version of CDT... :_(

  • Strange, this used to work with CDT 4. So if you really really need this I suggest you downgrade your CDT :-/

    divideandconquer.se : Maybe the formatter got too clever?
  • You might want to try the astyle eclipse plugin. It seems to be much nicer than the default eclipse style of C++ indentation.

0 comments:

Post a Comment