Click here to Skip to main content
15,891,864 members
Articles / Programming Languages / C++

Wave: a Standard conformant C++ preprocessor library

Rate me:
Please Sign up or sign in to vote.
4.96/5 (58 votes)
10 Jan 200413 min read 399.7K   4.4K   81  
Describes a free and fully Standard conformant C++ preprocessor library
# define CAT(a, b) PRIMITIVE_CAT(a, b)
# define PRIMITIVE_CAT(a, b) a ## b
#
# define OFFSET(n) OFFSET_ ## n
#
# define OFFSET_1 OFFSET_2 % 10
# define OFFSET_2 OFFSET_3 % 100
# define OFFSET_3 OFFSET_4 % 1000
# define OFFSET_4
#
# define FACTOR(n) FACTOR_ ## n
#
# define FACTOR_1 / 1
# define FACTOR_2 CAT(FACTOR_1, 0)
# define FACTOR_3 CAT(FACTOR_2, 0)
# define FACTOR_4 CAT(FACTOR_3, 0)
#
# define DIGIT(n) OFFSET(n) FACTOR(n)
#
# define x 987
#
# if (x) DIGIT(3) == 0
#    define D3 0
# elif (x) DIGIT(3)  == 1
#    define D3 1
# elif (x) DIGIT(3)  == 2
#    define D3 2
# elif (x) DIGIT(3)  == 3
#    define D3 3
# elif (x) DIGIT(3)  == 4
#    define D3 4
# elif (x) DIGIT(3)  == 5
#    define D3 5
# elif (x) DIGIT(3)  == 6
#    define D3 6
# elif (x) DIGIT(3)  == 7
#    define D3 7
# elif (x) DIGIT(3)  == 8
#    define D3 8
# elif (x) DIGIT(3)  == 9
#    define D3 9
# endif
#
# if (x) DIGIT(2) == 0
#    define D2 0
# elif (x) DIGIT(2)  == 1
#    define D2 1
# elif (x) DIGIT(2)  == 2
#    define D2 2
# elif (x) DIGIT(2)  == 3
#    define D2 3
# elif (x) DIGIT(2)  == 4
#    define D2 4
# elif (x) DIGIT(2)  == 5
#    define D2 5
# elif (x) DIGIT(2)  == 6
#    define D2 6
# elif (x) DIGIT(2)  == 7
#    define D2 7
# elif (x) DIGIT(2)  == 8
#    define D2 8
# elif (x) DIGIT(2)  == 9
#    define D2 9
# endif
#
# if (x) DIGIT(1) == 0
#    define D1 0
# elif (x) DIGIT(1)  == 1
#    define D1 1
# elif (x) DIGIT(1)  == 2
#    define D1 2
# elif (x) DIGIT(1)  == 3
#    define D1 3
# elif (x) DIGIT(1)  == 4
#    define D1 4
# elif (x) DIGIT(1)  == 5
#    define D1 5
# elif (x) DIGIT(1)  == 6
#    define D1 6
# elif (x) DIGIT(1)  == 7
#    define D1 7
# elif (x) DIGIT(1)  == 8
#    define D1 8
# elif (x) DIGIT(1)  == 9
#    define D1 9
# endif

D3 D2 D1

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States
Actively involved in Boost and the development of the Spirit parser construction framework.

Comments and Discussions