Skip to contentSkip to conversation

Function to fail at build time?

DISCUSSION Morten Brørup 2 participants 3 messages Jun 26, 2025

lore
Share & tools
lore
#0 lore raw

I was wondering if this was somehow possible:

#define RTE_VERIFY(exp) do { \
+ if (__rte_constant(exp)) \

+		FAIL_AT_BUILD_TIME(); \
	if (unlikely(!(exp))) \
		rte_panic("line %d\tassert \"%s\" failed\n", __LINE__, #exp); \
} while (0)

I tried static_assert(), but it cannot be used in this way.

-Morten

#1 lore raw
Quoted 10 lines · On Thu, 26 Jun 2025 01:55:02 +0200 Morten Brørup <[email protected]>…

I was wondering if this was somehow possible:

#define RTE_VERIFY(exp) do { \

(quoted diff) +2 -0
+	if (__rte_constant(exp)) \
+		FAIL_AT_BUILD_TIME(); \
	if (unlikely(!(exp))) \
		rte_panic("line %d\tassert \"%s\" failed\n", __LINE__, #exp); \
} while (0)

I tried static_assert(), but it cannot be used in this way.

What happens?

#2 lore raw
Quoted 14 lines

From: Stephen Hemminger [mailto:[email protected]]
Sent: Thursday, 26 June 2025 16.32

Quoted 7 lines · On Thu, 26 Jun 2025 01:55:02 +0200 Morten Brørup <[email protected]>…

I was wondering if this was somehow possible:

#define RTE_VERIFY(exp) do { \

(quoted diff) +2 -0
+	if (__rte_constant(exp)) \
+		FAIL_AT_BUILD_TIME(); \
	if (unlikely(!(exp))) \
		rte_panic("line %d\tassert \"%s\" failed\n", __LINE__,

#exp); \

Quoted 6 lines
Quoted 3 lines

} while (0)

I tried static_assert(), but it cannot be used in this way.

What happens?

The compiler says:
error: expected expression before '_Static_assert'

It seems static_assert is a declaration, so you cannot treat it as a function, using e.g.:
if (1) static_assert(1, "OK");