Representing big numbers in source code for readability?

后端 未结 5 2104
难免孤独
难免孤独 2020-11-27 23:23

Is there a more human-readable way for representing big numbers in the source code of an application written in C++ or C?

let\'s for example take the number 2,

5条回答
  •  心在旅途
    2020-11-28 00:12

    With Boost.PP:

    #define NUM(...) \
        NUM_SEQ(BOOST_PP_VARIADIC_TO_SEQ(__VA_ARGS__)) 
    #define NUM_SEQ(seq) \
        BOOST_PP_SEQ_FOLD_LEFT(NUM_FOLD, BOOST_PP_SEQ_HEAD(seq), BOOST_PP_SEQ_TAIL(seq)) 
    #define NUM_FOLD(_, acc, x) \
        BOOST_PP_CAT(acc, x)
    

    Usage:

    NUM(123, 456, 789) // Expands to 123456789
    

    Demo.

    Another way is making an UDL. Left as an exercise (and also because it requires more code).

提交回复
热议问题