Can you spawn an actor and add it to a level while in the editor using C++?
Yes, editor world is technically same as in gameplay, your actor is being placed in same way as you would spawn in gameplay, in fact actor class include editor code which code editor functions of actor and it’s behavior in it. Editor world has it’s own UWorld which you can use to spawn things in editor as you normally do. Only difference is that editor UWorld keeps actors in editor mode state, not fully initiated (BeginPlay is not being called as well as they not being ticked or physics processed etc).
You can check if actor is in editor world by checking UWorld type
Normally you dont need to do check as gameplay code is not being called in editor world and editor code is not being called in gameplay.
Also impotent note, make sure to put editor exclusive code in actor class inside this:
#if WITH_EDITOR
//...editor code here
#endif
portion inside will only build i editor builds of your module not in packaged game, some actor editor events are also filtered like that and you actually force to use this if you using them as those events won’t exist in packaged game.