Adam Ryczkowski
Adam Ryczkowski

Reputation: 8064

How to use BOOST_MPL_ASSERT

Why the following code fails to compile:

#include <boost/type_traits/is_same.hpp>
#include <boost/mpl/assert.hpp>
#include <string>

int main()
{
    BOOST_MPL_ASSERT(( boost::mpl::is_same<std::string, std::string> ));
    return 0;
}

I get an error for C++11 and C++14:

In file included from 2:0:
 In function 'int main()':
7:5: error: expected primary-expression before 'enum'

I understand it can be something silly, like missing semicolon. The error message is definitely not helpful.

Upvotes: 0

Views: 235

Answers (1)

Barry
Barry

Reputation: 303487

I agree that the error message is definitely not helpful.

The problem is you put the type trait in the wrong namespace. It's not boost::mpl::is_same, it's boost::is_same:

BOOST_MPL_ASSERT(( boost::is_same<std::string, std::string> ));

You cound find this out by trying to reduce the problem, by way of:

using T = boost::mpl::is_same<std::string, std::string>;

which gives a more helpful error:

foo.cxx: In function ‘int main()’:
foo.cxx:10:15: error: expected type-specifier
     using T = boost::mpl::is_same<std::string, std::string>;
               ^

If you're already on C++11, there's really not much reason to prefer BOOST_MPL_ASSERT to static_assert. Sure, you have to write out the ::type::value part yourself, but you can write a custom message and it's actually a part of the language.

Additionally in this case, the error message is even more helpful:

foo.cxx: In function ‘int main()’:
foo.cxx:10:19: error: ‘is_same’ is not a member of ‘boost::mpl’
     static_assert(boost::mpl::is_same<std::string, std::string>::type::value, "!");
                   ^

Upvotes: 1

Related Questions