Automatically split (refactor) .h into header and implementation (h+cpp)

前端 未结 4 1388
花落未央
花落未央 2021-01-02 00:06

When writing C++ code, I often start by writing full \'implementation\' code in my header files, then later need to refactor the implementation into a .cpp file.

Th

4条回答
  •  孤城傲影
    2021-01-02 00:49

    C++20 modules essentially do that for us

    As mentioned at: https://quuxplusone.github.io/blog/2019/11/07/modular-hello-world/ clang 2019-11 implements it along:

    clang++ -std=c++2a -c helloworld.cpp -Xclang -emit-module-interface -o helloworld.pcm
    clang++ -std=c++2a -c -fprebuilt-module-path=. -o helloworld.o helloworld.cpp
    clang++ -std=c++2a -fprebuilt-module-path=. -o main.out main.cpp helloworld.o
    

    where:

    • helloworld.cpp contains the implementation
    • helloworld.pcm is a precompiled module, basically an auto-extracted header from the .cpp (but in a clang internal language format) which gets used by main.cpp without an .hpp

    So basically clang is the tool, and thus parsing is perfect.

提交回复
热议问题