Replace C style comments by C++ style comments

2019-01-24 12:24发布

How can I automatically replace all C style comments (/* comment */) by C++ style comments (// comment)?

This has to be done automatically in several files. Any solution is okay, as long as it works.

标签: c++ comments
12条回答
Root(大扎)
2楼-- · 2019-01-24 12:36

There are a few suggestions that you might like to try out:

a)Write your own code (C/ Python/ any language you like) to replace the comments. Something along the lines of what regex said or this naive solution 'might' work: [Barring cases like the one rmeador, Darron posted]

 
for line in file:
    if line[0] == "\*":
       buf = '//' + all charachters in the line except '\*'
       flag = True
    if flag = True:
       if line ends with '*/':
          strip off '*/'
          flag = False
       add '//' + line to buf

b)Find a tool to do it. (I'll look up some and post, if I find them.)

c)Almost all modern IDE's (if you are using one) or text editors have an auto comment feature. You can then manually open up each file, select comment lines, decide how to handle the situation and comment C++ style using an accelerator (say Ctrl + M). Then, you can simply 'Find and Replace' all "/*" and "*/", again using your judgment. I have Gedit configured to do this using the "Code Comment' plugin. I don't remember the way I did it in Vim off hand. I am sure this one can be found easily.

  • 查看更多
    劳资没心,怎么记你
    3楼-- · 2019-01-24 12:37

    How do you intend to handle situations like this:

    void CreateExportableDataTable(/*[out, retval]*/ IDispatch **ppVal)
    {
     //blah
    }
    

    Note the comment inside the parens... this is a common way of documenting things in generated code, or mentioning default parameter values in the implementation of a class, etc. I'm usually not a fan of such uses of comments, but they are common and need to be considered. I don't think you can convert them to C++ style comments without doing some heavy thinking.

    查看更多
    别忘想泡老子
    4楼-- · 2019-01-24 12:38

    If you write an application/script to process the C source files, here are some things to be careful of:

    • comment characters within strings
    • comment characters in the middle of a line (you might not want to split the code line)

    You might be better off trying to find an application that understands how to actually parse the code as code.

    查看更多
    我想做一个坏孩纸
    5楼-- · 2019-01-24 12:42

    This tool does the job: http://people.sc.fsu.edu/~burkardt/cpp_src/recomment/recomment.html

    RECOMMENT is a C++ program which converts C style comments to C++ style comments.

    It also handles all the non-trivial cases mentioned by other people:

    This code incorporates suggestions and coding provided on 28 April 2005 by Steven Martin of JDS Uniphase, Melbourne Florida. These suggestions allow the program to ignore the internal contents of strings, (which might otherwise seem to begin or end comments), to handle lines of code with trailing comments, and to handle comments with trailing bits of code.

    查看更多
    神经病院院长
    6楼-- · 2019-01-24 12:48

    from PHP team convention... some reasonning has to exist if the question was asked. Just answer if you know.

    Never use C++ style comments (i.e. // comment). Always use C-style comments instead. PHP is written in C, and is aimed at compiling under any ANSI-C compliant compiler. Even though many compilers accept C++-style comments in C code, you have to ensure that your code would compile with other compilers as well. The only exception to this rule is code that is Win32-specific, because the Win32 port is MS-Visual C++ specific, and this compiler is known to accept C++-style comments in C code.

    查看更多
    SAY GOODBYE
    7楼-- · 2019-01-24 12:50

    I'm with the people who commented in your question. Why do it? Just leave it.

    it wastes time, adds useless commits to version control, risk of screwing up

    EDIT: Adding details from the comments from the OP

    The fundamental reason of preferring C++-style comment is that you can comment out a block of code which may have comments in it. If that comment is in C-style, this block-comment-out of code is not straight forward. – unknown (yahoo)

    that might be a fair/ok thing to want to do, but I have two comments about that:

    • I know of no one who would advocate changing all existing code - that is a preference for new code. (IMO)
    • If you feel the need to "comment out code" (another iffy practice) then you can do it as needed - not before

    It also appears that you want to use the c-style comments to block out a section of code? Or are you going to use the // to block out many lines?

    One alternative is a preprocessor #ifdef for that situation. I cringe at that but it is just as bad as commenting out lines/blocks. Neither should be left in the production code.

    查看更多
    登录 后发表回答