harhrayr's blog

By harhrayr, 12 years ago, In English

Does anybody know how to expand macros in visual c++ ?

For example.

#define FOR(i, n) for(i = 0; i < n; ++i)

It's faster to write FOR(i, n) than for(i = 0; i < n; ++i), but for(i = 0; i < n; ++i) is more

beautiful and comfortable, so I want to expand FOR(i, n) pressing a button or buttons. Is there

any extension for visual c++ 2010 or 2012 ?

Thanks.

  • Vote: I like it
  • +19
  • Vote: I do not like it

»
12 years ago, # |
  Vote: I like it 0 Vote: I do not like it

It's called code snippets, but I don't know how to use them in Visual C++. Try to google it.

  • »
    »
    12 years ago, # ^ |
    Rev. 2   Vote: I like it +8 Vote: I do not like it

    I know about snippets, there is for snippet in visual c++ 2012, but when you select it, you must write i and n. It takes a little bit more time.

    http://www.youtube.com/watch?v=j5cSxC6pKnI 2:41 , this is what I want.

    • »
      »
      »
      12 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I don't know about vs, bit usually you can configure macroses and print some text including variables, that you can change. so you should add macro with only pain text

      • »
        »
        »
        »
        12 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Yes it's possible, it can be done by creating a snippet, which prints plain text and then change the variables.

»
12 years ago, # |
  Vote: I like it +3 Vote: I do not like it

in Microsoft Visual studio Express 2012 write "for" and double tap the "TAB" button to automatically insert the for loop body for (int i = 0; i < length; i++) {

}

it is like the above after that you can change the "length" with anything you want

»
12 years ago, # |
  Vote: I like it +3 Vote: I do not like it

If you get used to the long version, it will be faster to write than short.

  • »
    »
    12 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    I think that the time spent typing in the solution is negligible altogether in comparison to the time spent reading, thinking, testing and debugging. Also, most people here are fast typists anyway. Thus, in my opinion, shorthand macros like FOR succeed only in making the code less readable, which leads to harder debugging.