Giter Site home page Giter Site logo

Comments (2)

arobenko avatar arobenko commented on July 19, 2024

Hi @ponast,
I try not to abuse usage of constexpr in my work in general, and it CommsChampion Ecosystem in particular. Although C++ is quite flexible in allowing indiscriminate usage of this keyword and spits out errors only when the result of constexpr function is attempted to be used for compile time code generation conditions (such as using classes from type_traits), but evaluation of the return value at compile time cannot be calculated.

My convention is to add constexpr only for functions that are expected to be used for compile time evaluation, mostly for boolean expressions of std::conditional<...> or similar. It becomes a "contract" for the function that it's usage of the compile time evaluation will never fail. Once constexpr - always constexpr. Anything else I leave for the regular compiler optimizations.

As for the suggested change, the constexpr on the return type of const char* doesn't make much sense, it doesn't preserve the size of the string, i.e. it's not const char[N]. To allow the compile time strings the name should be a static variable, rather than a function

static constexpr const char NameStr[] = "some_name";

However, my experiments show that some compilers (clang for example) don't handle this case properly. The link stage reports error of inability to find the string symbol:

/usr/bin/ld: /usr/bin/ld: DWARF error: invalid or unhandled FORM value: 0x23
...:(.text+0x14e2e): undefined reference to `SomeField::NameStr'
clang: error: linker command failed with exit code 1 (use -v to see invocation)

However if the constexpr string is put in the anonymous namespace in the cpp file instead of the static member of the class, everything compiles normally.

Also worth mentioning that when developing initial versions of the code generator I intended to make sure that the strings are not duplicated, i.e. having single class accumulating all the strings to avoid repetition. However, when analyzing the binary code produced by the compiler I noticed that it cleans up all the duplicates by itself during its optimization stage, i.e. the following classes will reference the same and the only "SomeName" string in the read only section of the binary.

struct SomeField
{
    static const char* name()
    {
        return "SomeName";
    }
};

struct SomeOtherField
{
    static const char* name()
    {
        return "SomeName";
    }
};

As the result complicating the code for strings storage wasn't worth the effort.

Another thing: I personally use CommsChampion Ecosystem at work for the old product that is compiled using quite old gcc-4.8. So I put a significant effort not to introduce code supported by C++14/17/20, but not C++11 or having difficulties being compiled with gcc-4.8. At least not at this stage.


The bottom line, I don't think your suggestion of adding constexpr to the signature of name() member function has any (significant) added value.

from commsdsl.

ponast avatar ponast commented on July 19, 2024

Thanks for the thorough elaboration. You definitely have considered the topic in more depth than I have. I will take a look at my own use of constexpr which until now has been indiscriminate.

from commsdsl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.