Can I write C++ code without headers (repetitive function declarations)?

前端 未结 24 716
半阙折子戏
半阙折子戏 2020-11-28 07:55

Is there any way to not have to write function declarations twice (headers) and still retain the same scalability in compiling, clarity in debugging, and flexibility in desi

相关标签:
24条回答
  • 2020-11-28 08:24

    Sorry, but there's no such thing as a "best practice" for eliminating headers in C++: it's a bad idea, period. If you hate them that much, you have three choices:

    • Become intimately familiar with C++ internals and any compilers you're using; you're going to run into different problems than the average C++ developer, and you'll probably need to solve them without a lot of help.
    • Pick a language you can use "right" without getting depressed
    • Get a tool to generate them for you; you'll still have headers, but you save some typing effort
    0 讨论(0)
  • 2020-11-28 08:24

    This has been "revived" thanks to a duplicate...

    In any case, the concept of a header is a worthy one, i.e. separate out the interface from the implementation detail. The header outlines how you use a class / method, and not how it does it.

    The downside is the detail within headers and all the workarounds necessary. These are the main issues as I see them:

    • dependency generation. When a header is modified, any source file that includes this header requires recompilation. The issue is of course working out which source files actually use it. When a "clean" build is performed it is often necessary to cache the information in some kind of dependency tree for later.

    • include guards. Ok, we all know how to write these but in a perfect system it would not be necessary.

    • private details. Within a class, you must put the private details into the header. Yes, the compiler needs to know the "size" of the class, but in a perfect system it would be able to bind this in a later phase. This leads to all kinds of workaround like pImpl and using abstract base classes even when you only have one implementation just because you want to hide a dependency.

    The perfect system would work with

    • separate class definition and declaration
    • A clear bind between these two so the compiler would know where a class declaration and its definition are, and would know what the size of a class.
    • You declare using class rather than pre-processor #include. The compiler knows where to find a class. Once you have done "using class" you can use that class name without qualifying it.

    I'd be interested to know how D does it.

    With regards to whether you can use C++ without headers, I would say no you need them for abstract base classes and standard library. Aside from that you could get by without them, although you probably would not want to.

    0 讨论(0)
  • 2020-11-28 08:25

    You have to write function declaration twice, actually (once in header file, once in implementation file). The definition (AKA implementation) of the function will be written once, in the implementation file.

    You can write all the code in header files (it is actually a very used practice in generic programming in C++), but this implies that every C/CPP file including that header will imply recompilation of the implementation from those header files.

    If you are thinking to a system similar to C# or Java, it is not possible in C++.

    0 讨论(0)
  • 2020-11-28 08:25

    C++ 20 modules solve this problem. There is no need for copy-pasting anymore! Just write your code in a single file and export things using "export".

    export module mymodule;
    
    export int myfunc() {
        return 1
    }
    

    Read more about modules here: https://en.cppreference.com/w/cpp/language/modules

    At the time of writing this answer, these compilers support it:

    See here for the supported compilers: https://en.cppreference.com/w/cpp/compiler_support

    0 讨论(0)
  • 2020-11-28 08:25

    You can do without headers. But, why spend effort trying to avoid carefully worked out best practices that have been developed over many years by experts.

    When I wrote basic, I quite liked line numbers. But, I wouldn't think of trying to jam them into C++, because that's not the C++ way. The same goes for headers... and I'm sure other answers explain all the reasoning.

    0 讨论(0)
  • 2020-11-28 08:26

    Actually... You can write the entire implementation in a file. Templated classes are all defined in the header file with no cpp file.

    You can also save then with whatever extensions you want. Then in #include statements, you would include your file.

    /* mycode.cpp */
    #pragma once
    #include <iostreams.h>
    
    class myclass {
    public:
      myclass();
    
      dothing();
    };
    
    myclass::myclass() { }
    myclass::dothing()
    {
      // code
    }
    

    Then in another file

    /* myothercode.cpp */
    #pragma once
    #include "mycode.cpp"
    
    int main() {
       myclass A;
       A.dothing();
       return 0;
    }
    

    You may need to setup some build rules, but it should work.

    0 讨论(0)
提交回复
热议问题