UPDATE: I created Semantic Refactor package that completely solves this problem and more. You can look at the demos to see how it works. The remaining text of this answer, after this sentence, is old and put it there just for historical reason.
You can use
senator-copy-tag for accurately copy function signature, then paste back in your source file.
senator-copy-tag and Senator commands are available when you enable
Semantic is an Emacs built-in package.
You can combine Semantic Senator with Projectile into a command to insert function prototype into other file (file with the same name but different extension) from anywhere in your project. If there's only one other file, the command inserts immediately into that file; if more than one, you are prompted to select a file; if there's none, you are prompted for the entire files in your project. After you select a file, a prompt offers a list of Semantic tags in the current buffer that you can choose to insert after.
I submitted a PR to Emacs Refactor. Full code if you want to try without waiting for the PR: click here.
Here is a demo (it starts when you see
START DEMO at the bottom):
You can also use only Senator to copy and function prototype. As long as point is anywhere inside the function signature or function body, run
senator-copy-tag, which is bound to
C-c , M-w by default, it copies the whole function: both the signature and body. However, you can paste only the signature if you want by running the command
senator-yank-tag, which is bound to
C-c , C-y by default. Pressing
C-y paste the whole function signature along with its body.
senator-copy-tag even works with function signature expanded on multiple lines like this:
Although this approach does not insert directly into buffer with the same name, it is more applicable in other cases. Your use case only works if you have two file in the same directory and with the same name but different extensions. What if the function declaration and function definition must stay in different files with different names?
EDIT2: Here is an example for smart insertion of function prototype using Semantic tags. Currently, you can only insert based on the relative positions ("before" and "after") of top level Semantic tags. I will update to make user to be able to insert in any place where Semantic tags are available, with more positions (i.e. when a tag is a
Class, it should offer additional positions:
private). The demo starts when you see
START DEMO at the bottom:
Bonus: If you want to generate a list of empty function definitions in a
.cpp file from a header file, use member-functions.el. But soon, I will replace it with Semantic + Projectile.