Use compile-time constants instead of unnamed enumerations (remove "enum hack") #10749

pull practicalswift wants to merge 1 commits into bitcoin:master from practicalswift:enum-hack changing 5 files +14 −20
  1. practicalswift commented at 2:51 PM on July 5, 2017: contributor

    Use compile-time constants instead of unnamed enumerations (remove "enum hack").

  2. fanquake added the label Refactoring on Jul 5, 2017
  3. jonasschnelli commented at 4:13 PM on July 5, 2017: contributor

    utACK 0f3d58be40672151468700fb5a05898468814880

  4. jgarzik commented at 5:43 PM on July 5, 2017: contributor

    This enum technique is a well-defined way to generate typed constants.

    Is it causing problems somewhere?

  5. TheBlueMatt commented at 6:22 PM on July 5, 2017: member

    utACK 0f3d58be40672151468700fb5a05898468814880

  6. gmaxwell approved
  7. gmaxwell commented at 12:53 AM on July 6, 2017: contributor

    ACK.

  8. benma commented at 11:44 AM on July 25, 2017: none

    First time I read about constexpr, and I also googled for enum vs constexpr. For what it's worth, I've seen quite a few threads that lean towards enums even with constexpr being available, like https://stackoverflow.com/a/37259949.

    constexpr is easier to read, and it doesn't seem to have a downside at least in this PR.

  9. practicalswift commented at 11:50 AM on July 25, 2017: contributor

    @benma I take that as an utACK? :-)

  10. Use compile-time constants instead of unnamed enumerations (remove "enum hack") 1e65f0f339
  11. in src/consensus/consensus.h:33 in 0f3d58be40 outdated
      30 | -    /* Interpret sequence numbers as relative lock-time constraints. */
      31 | -    LOCKTIME_VERIFY_SEQUENCE = (1 << 0),
      32 | -
      33 | -    /* Use GetMedianTimePast() instead of nTime for end point timestamp. */
      34 | -    LOCKTIME_MEDIAN_TIME_PAST = (1 << 1),
      35 | -};
    


    benma commented at 4:11 PM on July 25, 2017:

    Isn't an enum appropriate when it's indeed an enumeration?

    Aso, please rebase, as there are other consts that could be constexprs in consensus.h now:

    static const size_t MIN_TRANSACTION_WEIGHT = WITNESS_SCALE_FACTOR * 60; // 60 is the lower bound for the size of a valid serialized CTransaction
    static const size_t MIN_SERIALIZABLE_TRANSACTION_WEIGHT = WITNESS_SCALE_FACTOR * 10; // 10 is the lower bound for the size of a serialized CTransaction
    

    Still a bit confused about the whole thing, seems like simple const would also suffice. Is the advantage of constexpr just that it gives a compile time error in case the expression can't be evaluated at compile time?


    practicalswift commented at 10:30 PM on July 25, 2017:
    1. Sure, that case could be made. I can do it as a scoped named enum (enum class). Any suggestion on a proper name for that enumeration?
    2. The goal of this PR is to get rid of enum hacks. Changing existing const:s to constexpr:s is not within that scope :-)
    3. When introducing new compile-time constants I prefer constexpr which is explicit about the intention of the code (getting a compile-time constant). Do you see any compelling reasons to avoid using constexpr (which implies const) for compile-time constants?

    benma commented at 2:52 PM on July 26, 2017:

    Thanks. I just thew in some thoughts in case you can pick something from it. I have no preference either way.

  12. practicalswift force-pushed on Jul 25, 2017
  13. practicalswift commented at 10:15 PM on July 25, 2017: contributor

    Rebased!

  14. benma commented at 2:51 PM on July 26, 2017: none

    utACK 1e65f0f3396d5d7eaa8e8dd3668dfa180b541c18

  15. jnewbery commented at 4:33 PM on August 17, 2017: member

    ACK 1e65f0f3396d5d7eaa8e8dd3668dfa180b541c18

  16. MarcoFalke commented at 5:33 PM on November 11, 2017: member
    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA512
    
    utACK 1e65f0f3396d5d7eaa8e8dd3668dfa180b541c18
    -----BEGIN PGP SIGNATURE-----
    
    iQIcBAEBCgAGBQJaBzQQAAoJENLqSFDnUosllrgP/0Jq2acX/JFfphTWDMcGzChi
    NtMamQFeV1gvGLvUYW7UogT6R1BGCCCJ92OeVZFBQWsLo5ZkrucxQcUYKDnV5gKi
    7LCl/8rz6LWmPvJszFPUjvE7/X5Nd6r1crCZJ9oPaO/w4tK8k5UeeI+ODn6+XUFN
    WaoqP/sxnfeDm90fDybU7aix0yCaDsLZ/rHl6zVFSLQerb4McKG7ji/MjW4Bi/ii
    wCAYHc9Gzfxa3y3AMaiA+zh/GMSLDBOvLaClWt3UlXI9pfVA9giP+PPgtKAHkz82
    2lX+byUyGVK3n5oSBeJD8A4mmcOMvF0DT+6XlvlETX2ApnzZlkzX7tqtDa0w3UtJ
    Ni1iCzZl11tgqXRTpQYaxTL5YdoDbeudhvz5gmZ+N2wNLIVcmbJSztt62eQI00GW
    G5LPnWrDtaDOKn7Rv6kVJkgAcel9nIrIibvosQi0anrwVGZ3Fg7GXKY3kEhgwIuO
    2YmjXJ5xEhwUDktopn7/Mt/fSeMWeg0Gg7oqGiJ6WCY0nYC9gHPJBd8k9nH/pOCP
    V61mWhpDRjp+bQZvS6IL0GkQEJ/IPxPgTE4xNrsYMpVgBBPiWhjCy6VnAPbxZHd9
    xJVpjJC+QKwR+zfVpoF2VzWKFlf28cfNSNh9z+8q6Juvv6maYUxlPxQ4zgMms8LA
    1zrcJiqm/1ySme7GeWhb
    =a+3Y
    -----END PGP SIGNATURE-----
    
    
  17. MarcoFalke merged this on Nov 11, 2017
  18. MarcoFalke closed this on Nov 11, 2017

  19. MarcoFalke referenced this in commit 2adbddb038 on Nov 11, 2017
  20. PastaPastaPasta referenced this in commit 23074882e7 on Jan 17, 2020
  21. PastaPastaPasta referenced this in commit f275b3fc70 on Jan 22, 2020
  22. PastaPastaPasta referenced this in commit aeaf3c5ef9 on Jan 22, 2020
  23. PastaPastaPasta referenced this in commit 3ab405a96b on Jan 29, 2020
  24. ckti referenced this in commit 7c42b740fd on Mar 28, 2021
  25. practicalswift deleted the branch on Apr 10, 2021
  26. gades referenced this in commit 1a88238cf4 on Jun 26, 2021
  27. furszy referenced this in commit 60d36292bc on Jul 26, 2021
  28. gades referenced this in commit 03df621768 on Feb 5, 2022
  29. DrahtBot locked this on Aug 16, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-16 15:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me