<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Static Assert]]></title><description><![CDATA[<p>Da ich keinen Gebrauch von C++11 machen kann muss ich mir ein Workaround für ein static_assert bauen. Ich möchte ungern Templates mit Preprozessor Direktiven mischen, weiss aber keine bessere Lösung als das Macro SOMETHING_IS_OK() enzusetzen. Gibts hier eine alternative Implementierung ohne Verwendung eines Macros?</p>
<pre><code class="language-cpp">#include &lt;assert.h&gt;  // assert

// Alternative implementation for static_assert() C++11.
template&lt;bool T&gt;
class StaticAssert;
template&lt;&gt;
class StaticAssert&lt;true&gt; {};
#define STATIC_ASSERT(expression) StaticAssert&lt;(expression)&gt;()

// Returns false to trigger a compile time assertion.
#define SOMETHING_IS_OK(x) ((x) == 123456 || (x) == 234567)

// Returns false to trigger a runtime assertion.
inline bool SomethingIsOK(int x) {
  return x == 123456 || x == 234567;
}

template&lt;int COMPILE_TIME&gt;
void DoNothing(int runtime) {
  STATIC_ASSERT(SOMETHING_IS_OK(COMPILE_TIME));
  assert(SomethingIsOK(runtime));
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/305073/static-assert</link><generator>RSS for Node</generator><lastBuildDate>Thu, 25 Jun 2026 11:21:38 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/305073.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 20 Jun 2012 12:27:04 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Static Assert on Wed, 20 Jun 2012 12:28:35 GMT]]></title><description><![CDATA[<p>Da ich keinen Gebrauch von C++11 machen kann muss ich mir ein Workaround für ein static_assert bauen. Ich möchte ungern Templates mit Preprozessor Direktiven mischen, weiss aber keine bessere Lösung als das Macro SOMETHING_IS_OK() enzusetzen. Gibts hier eine alternative Implementierung ohne Verwendung eines Macros?</p>
<pre><code class="language-cpp">#include &lt;assert.h&gt;  // assert

// Alternative implementation for static_assert() C++11.
template&lt;bool T&gt;
class StaticAssert;
template&lt;&gt;
class StaticAssert&lt;true&gt; {};
#define STATIC_ASSERT(expression) StaticAssert&lt;(expression)&gt;()

// Returns false to trigger a compile time assertion.
#define SOMETHING_IS_OK(x) ((x) == 123456 || (x) == 234567)

// Returns false to trigger a runtime assertion.
inline bool SomethingIsOK(int x) {
  return x == 123456 || x == 234567;
}

template&lt;int COMPILE_TIME&gt;
void DoNothing(int runtime) {
  STATIC_ASSERT(SOMETHING_IS_OK(COMPILE_TIME));
  assert(SomethingIsOK(runtime));
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2225362</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2225362</guid><dc:creator><![CDATA[Tomahawk]]></dc:creator><pubDate>Wed, 20 Jun 2012 12:28:35 GMT</pubDate></item><item><title><![CDATA[Reply to Static Assert on Wed, 20 Jun 2012 12:31:00 GMT]]></title><description><![CDATA[<p>Dann lass das Makro doch weg und benutz das Template direkt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2225367</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2225367</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Wed, 20 Jun 2012 12:31:00 GMT</pubDate></item></channel></rss>