1

Compiles fine with gcc (GCC) 4.1.2 20080704 (Red Hat 4.1.2-44)/ boost 1.33.1

#include <boost/mpl/assert.hpp>

int main()
{
    BOOST_MPL_ASSERT(( mpl::equal_to< mpl::long_<10>, mpl::int_<11> > ));
}

After preprocessing main() looks like:

int main()
{
    enum { mpl_assertion_in_line_5 = sizeof( boost::mpl::assertion_failed<false>( boost::mpl::assert_arg( (void (*) ( mpl::equal_to< mpl::long_<10>, mpl::int_<11> > ))0, 1 ) ) ) };
}

What is wrong?

dimba
  • 26,717
  • 34
  • 141
  • 196
  • I don't understand the question though... – maverik Apr 14 '11 at 06:36
  • IMHO it should fail to compile, since 10 != 11 – dimba Apr 14 '11 at 06:38
  • Interesting - on MinGW 4.5.2 I have the opposite problem - a compiler error whether or not the assertion should fail (ie., I get the same error if I use `long_<10>` for both arguments). With your example, I get the same preprocessor output in `main()`, but I get the following errors: `error: expected primary-expression before 'enum'` and `error: expected ';' before 'enum'`. But I get the same errors even when the assertion shouldn't fail. Unfortunately the MPL stuff is far to cryptic for me to debug. – Michael Burr Apr 14 '11 at 07:23
  • By the way, I'm using Boost 1.46. The version you're using (1.33.1) is pretty dang old. I'd suggest trying something newer, even though I'm not getting good results either. – Michael Burr Apr 14 '11 at 07:28
  • @Michel same output with 1.45. – dimba Apr 14 '11 at 07:34
  • @Michel Regarding error " expected primary-expression before 'enum'" I also encountered it when trying to use BOOST_MPL_ASSERT in class definition. When I passed assert to c-tor, error disappeared. IMHO I can use assert in class declaration too. Right? – dimba Apr 14 '11 at 07:35
  • @dimba: I'm far from an MPL expert, but it seems like the MPL assert is intended to be allowed anywhere an `enum` declaration is allowed, since that's what it boils down to. So pretty much anywhere except as part of an expression. – Michael Burr Apr 14 '11 at 08:16

1 Answers1

3

You could be missing some headers (and this might depend on the exact version of boost - I don't have that one available). This fails to compile properly:

#include <boost/mpl/int.hpp>
#include <boost/mpl/long.hpp>
#include <boost/mpl/equal_to.hpp>
#include <boost/mpl/assert.hpp>

int main()
{
    BOOST_MPL_ASSERT(( boost::mpl::equal_to< boost::mpl::long_<10>, boost::mpl::int_<11> > ));
}

The output is:

# g++ -Wall t.cpp
t.cpp: In function ‘int main()’:
t.cpp:8:2: error: no matching function for call to ‘assertion_failed(mpl_::failed************ boost::mpl::equal_to<mpl_::long_<10l>, mpl_::int_<11> >::************)’

Without the proper headers, various other unrelated compile errors happen.

If you don't want the namespace qualifiers, you can do:

#include <boost/mpl/int.hpp>
#include <boost/mpl/long.hpp>
#include <boost/mpl/equal_to.hpp>
#include <boost/mpl/assert.hpp>

using namespace boost; // brings boost:: into scope

int main()
{
    BOOST_MPL_ASSERT(( mpl::equal_to< mpl::long_<10>, mpl::int_<11> > ));
}

Or even:

#include <boost/mpl/int.hpp>
#include <boost/mpl/long.hpp>
#include <boost/mpl/equal_to.hpp>
#include <boost/mpl/assert.hpp>

using namespace boost::mpl; // brings boost::mpl:: in scope

int main()
{
    BOOST_MPL_ASSERT(( equal_to< long_<10>, int_<11> > ));
}
Mat
  • 202,337
  • 40
  • 393
  • 406
  • 1
    Ok, if I use boost qualifier (e.g. instead of mpl::equal_to boost::mpl::equal_to) assert works as expected, event with one header file in question!!! But main with "boost::" and without it is same - all mpl's are "boost" qualified. I'm confused? without "boost::" it should fail to compile? – dimba Apr 14 '11 at 08:02
  • I don't understand what you're asking in your comment. I edited my answer to show how you can use the `boost::` and `boost::mpl::` namespaces directly. If your namespace qualifiers are wrong or incomplete, and you're not using the correct `using`-directive, the compiler will fail to understand the types you're talking about, and the mpl_assert works on types - so the compiler will be confused and tell you about those other type errors or something unrelated if the corresponding macro expansion ends up not being valid. – Mat Apr 14 '11 at 09:10
  • What I mean that my code in question compiles as is and the code doesn't introduce any namespaces. So how compiler knows what is "mpl::equal_to"? I expect it not compile. Next question is how in preprocessed code "mpl::equal_to"/"mpl::long_" becomes "boost::" qualified? – dimba Apr 14 '11 at 13:14
  • @dimba: your code **does not** compile, precisely because it is wrong and is missing the correct namespace (and possibly includes). It still does not compile even if you put the exact same types inside the equal_to template. If you fix your namespace problems (and include the correct header), your code will start to fail to compile as intended (i.e. will break with different types in equal_to, but compile properly with the same types). – Mat Apr 14 '11 at 13:19