Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
Post History
I developed this macro (using GNU C) similar to C++'s const_cast(). #define const_cast(t, p) \ ({ ...
Answer
#6: Post edited
- I developed this macro (using GNU C) similar to C++'s const_cast().
- ```c
#define const_cast(t, p) \({ \static_assert(__builtin_types_compatible_p(typeof(&*p), const t)); \(t) (p); \- })
- ```
- It asserts that the `const` version of the specified type is compatible with the type of `p`. Then it does the cast.
- However, as @Lundin said, if you need this macro, you probably have a serious design issue in your code.
- I developed this macro (using GNU C) similar to C++'s const_cast().
- ```c
- #define const_cast(t, p) \
- ({ \
- static_assert(__builtin_types_compatible_p(typeof(&*(p)), const t)); \
- (t) (p); \
- })
- ```
- It asserts that the `const` version of the specified type is compatible with the type of `p`. Then it does the cast.
- However, as @Lundin said, if you need this macro, you probably have a serious design issue in your code.
#5: Post edited
- I developed this macro (using GNU C) similar to C++'s const_cast().
- ```c
#define const_cast(t, p) \({ \- static_assert(__builtin_types_compatible_p(typeof(&*p), const t)); \
- (t) (p); \
- })
- ```
- It asserts that the `const` version of the specified type is compatible with the type of `p`. Then it does the cast.
- However, as @Lundin said, if you need this macro, you probably have a serious design issue in your code.
- I developed this macro (using GNU C) similar to C++'s const_cast().
- ```c
- #define const_cast(t, p) \
- ({ \
- static_assert(__builtin_types_compatible_p(typeof(&*p), const t)); \
- (t) (p); \
- })
- ```
- It asserts that the `const` version of the specified type is compatible with the type of `p`. Then it does the cast.
- However, as @Lundin said, if you need this macro, you probably have a serious design issue in your code.
#4: Post edited
- I developed this macro (using GNU C) similar to C++'s const_cast().
- ```c
- #define const_cast(t, p) \
- ({ \
- static_assert(__builtin_types_compatible_p(typeof(&*p), const t)); \
(t) (p); \- })
- ```
- It asserts that the `const` version of the specified type is compatible with the type of `p`. Then it does the cast.
- However, as @Lundin said, if you need this macro, you probably have a serious design issue in your code.
- I developed this macro (using GNU C) similar to C++'s const_cast().
- ```c
- #define const_cast(t, p) \
- ({ \
- static_assert(__builtin_types_compatible_p(typeof(&*p), const t)); \
- (t) (p); \
- })
- ```
- It asserts that the `const` version of the specified type is compatible with the type of `p`. Then it does the cast.
- However, as @Lundin said, if you need this macro, you probably have a serious design issue in your code.
#3: Post edited
- I developed this macro (using GNU C) similar to C++'s const_cast().
- ```c
- #define const_cast(t, p) \
- ({ \
static_assert(__builtin_types_compatible_p(typeof(p), const t)); \(t) p; \- })
- ```
- It asserts that the `const` version of the specified type is compatible with the type of `p`. Then it does the cast.
- However, as @Lundin said, if you need this macro, you probably have a serious design issue in your code.
- I developed this macro (using GNU C) similar to C++'s const_cast().
- ```c
- #define const_cast(t, p) \
- ({ \
- static_assert(__builtin_types_compatible_p(typeof(&*p), const t)); \
- (t) (p); \
- })
- ```
- It asserts that the `const` version of the specified type is compatible with the type of `p`. Then it does the cast.
- However, as @Lundin said, if you need this macro, you probably have a serious design issue in your code.
#2: Post edited
I developed this macro similar to C++'s const_cast().- ```c
- #define const_cast(t, p) \
- ({ \
- static_assert(__builtin_types_compatible_p(typeof(p), const t)); \
- (t) p; \
- })
- ```
- It asserts that the `const` version of the specified type is compatible with the type of `p`. Then it does the cast.
- However, as @Lundin said, if you need this macro, you probably have a serious design issue in your code.
- I developed this macro (using GNU C) similar to C++'s const_cast().
- ```c
- #define const_cast(t, p) \
- ({ \
- static_assert(__builtin_types_compatible_p(typeof(p), const t)); \
- (t) p; \
- })
- ```
- It asserts that the `const` version of the specified type is compatible with the type of `p`. Then it does the cast.
- However, as @Lundin said, if you need this macro, you probably have a serious design issue in your code.
#1: Initial revision
I developed this macro similar to C++'s const_cast(). ```c #define const_cast(t, p) \ ({ \ static_assert(__builtin_types_compatible_p(typeof(p), const t)); \ (t) p; \ }) ``` It asserts that the `const` version of the specified type is compatible with the type of `p`. Then it does the cast. However, as @Lundin said, if you need this macro, you probably have a serious design issue in your code.