Boost C++ Libraries: Ticket #5615: BOOST_CHECK_EQUAL does not work well with const static members https://svn.boost.org/trac10/ticket/5615 <p> I have tried to create set of test cases using BOOST_AUTO_TEST_CASE_TEMPLATE() macro. Each tested class shares the same interface, but values returned from functions are different. Therefore I decided to use helper traits template to avoid code duplication. Each traits struct contains arguments and expected results for tested classes, stored as static const values - see example below. Unfortunately this code does not link successfully - linker complains about missing static struct members. Fortunately workaround is simple - just copy expected value to temporary variable and use it in BOOST_CHECK_EQUAL(). </p> <p> Please fix this to allow this code to compile and link successfully, without using workaround. </p> <pre class="wiki">class SomeClass { public: int test(int arg) { return arg + 1; } }; BOOST_AUTO_TEST_SUITE( Test_err ) template&lt;typename TClass&gt; struct Tested_class_traits; template&lt;&gt; struct Tested_class_traits&lt;SomeClass&gt; { static const int arg = 1; static const int result = 2; }; typedef boost::mpl::list&lt; SomeClass &gt; Tested_types; BOOST_AUTO_TEST_CASE_TEMPLATE( Test_err_test, TClass, Tested_types ) { typedef Tested_class_traits&lt;TClass&gt; TestTraits; TClass obj; BOOST_CHECK_EQUAL( obj.test(TestTraits::arg), TestTraits::result ); } BOOST_AUTO_TEST_SUITE_END() </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5615 Trac 1.4.3 anonymous Thu, 16 Jun 2011 17:01:04 GMT <link>https://svn.boost.org/trac10/ticket/5615#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5615#comment:1</guid> <description> <p> Here is the linker error: </p> <pre class="wiki">/path/to/Test_err.cc:34: undefined reference to `Test_err::Tested_class_traits&lt;SomeClass&gt;::result' </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Steven Watanabe</dc:creator> <pubDate>Sun, 16 Oct 2011 23:11:53 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/5615#comment:2 https://svn.boost.org/trac10/ticket/5615#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">wontfix</span> </li> </ul> <p> This is not unique to Boost.Test. An out of line definition of a static constant is required in some circumstances. You can often get away without it, but it's safest just to provide it all the time. </p> Ticket anonymous Sun, 27 Jan 2013 23:15:16 GMT <link>https://svn.boost.org/trac10/ticket/5615#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5615#comment:3</guid> <description> <p> Replying to <a class="ticket" href="https://svn.boost.org/trac10/ticket/5615#comment:2" title="Comment 2">steven_watanabe</a>: </p> <blockquote class="citation"> <p> This is not unique to Boost.Test. An out of line definition of a static constant is required in some circumstances. You can often get away without it, but it's safest just to provide it all the time. </p> </blockquote> <p> Hi, I've just tried two very similar test cases: </p> <pre class="wiki">#define BOOST_TEST_MODULE geez #include &lt;boost/test/unit_test.hpp&gt; #include &lt;boost/tuple/tuple.hpp&gt; BOOST_AUTO_TEST_SUITE(foo) BOOST_AUTO_TEST_CASE(bar) { using boost::tuples::length; using boost::tuples::tuple; BOOST_CHECK_EQUAL( (length&lt;tuple&lt;int, char&gt; &gt;::value), 2); } BOOST_AUTO_TEST_SUITE_END() </pre><p> this program won't link: </p> <pre class="wiki">ptomulik@barakus:$ g++ -DBOOST_TEST_DYN_LINK=1 -ansi -Werror -Wall -Wextra -pedantic b.cpp -lboost_unit_test_framework /tmp/cc1snUGj.o: In function `foo::bar::test_method()': b.cpp:(.text+0x188): undefined reference to `boost::tuples::length&lt;boost::tuples::tuple&lt;int, char, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type, boost::tuples::null_type&gt; &gt;::value' collect2: error: ld returned 1 exit status </pre><p> Quite suprising, but when I replace BOOST_CHECK_EQUAL(a,b) with BOOST_CHECK(a == b), the program compiles and links smoothly: </p> <pre class="wiki">#define BOOST_TEST_MODULE geez #include &lt;boost/test/unit_test.hpp&gt; #include &lt;boost/tuple/tuple.hpp&gt; BOOST_AUTO_TEST_SUITE(foo) BOOST_AUTO_TEST_CASE(bar) { using boost::tuples::length; using boost::tuples::tuple; BOOST_CHECK( (length&lt;tuple&lt;int, char&gt; &gt;::value) == 2); } BOOST_AUTO_TEST_SUITE_END() </pre><pre class="wiki">ptomulik@barakus:$ g++ -DBOOST_TEST_DYN_LINK=1 -ansi -Werror -Wall -Wextra -pedantic b.cpp -lboost_unit_test_framework ptomulik@barakus:$ </pre><p> This is quite strange, and looks like bug in boost test framework, boost tuple implementation or elsewhere. </p> <p> Note, that very similar code with BOOST_CHECK_EQUAL but for std::tuple compiles and links just fine: </p> <pre class="wiki">#define BOOST_TEST_MODULE geez #include &lt;boost/test/unit_test.hpp&gt; #include &lt;tuple&gt; BOOST_AUTO_TEST_SUITE(foo) BOOST_AUTO_TEST_CASE(bar) { using std::tuple_size; using std::tuple; BOOST_CHECK_EQUAL( (tuple_size&lt;tuple&lt;int, char&gt; &gt;::value), 2); } BOOST_AUTO_TEST_SUITE_END() </pre><pre class="wiki">ptomulik@barakus:$ g++ -DBOOST_TEST_DYN_LINK=1 -ansi -std=c++11 -Werror -Wall -Wextra -pedantic b.cpp -lboost_unit_test_framework ptomulik@barakus:$ </pre><p> </p> </description> <category>Ticket</category> </item> </channel> </rss>