Question

I tried boost::mpl recently and it seems both awesome and horrible. Sometimes the compilation error information is rather confusing.

This time I get problem on the following code:

#include <iostream>
#include <boost/type_traits/is_same.hpp>
#include <boost/mpl/integral_c_tag.hpp>
#include <boost/mpl/tag.hpp>
#include <typeinfo>
#include <boost/mpl/for_each.hpp>
#include <boost/mpl/range_c.hpp>
#include <boost/mpl/vector.hpp>
#include <boost/mpl/copy.hpp>

//Using metafunction tag<> to acquire type so that mpl will only output integers.

struct mpl_func2
{
    template<typename T>
    void operator()(T t)
    {
        if(boost::is_same<boost::mpl::tag<T>::type, boost::mpl::integral_c_tag>::value)
        {cout<<t<<',';} 
    }
};

Here is the error messages:

error: type/value mismatch at argument 1 in template parameter list for 'template struct boost::is_same'

error: expected a type, got 'boost::mpl::tag::type'

Was it helpful?

Solution

You should use

typename boost::mpl::tag<T>::type

since type is dependent-name. Read more information about it here

OTHER TIPS

Unfortunately, you are mixing the concepts of compile-time and runtime programming.

In the Template Metaprogramming world, you would use something like enable_if (http://en.cppreference.com/w/cpp/types/enable_if) to accomplish what you want.

In that page there are numerous examples that will illustrate how you can select which implementation to execute at runtime, based on your compile-time types.

I would also suggest reading Dave Abraham's book on MPL (http://www.amazon.com/Template-Metaprogramming-Concepts-Techniques-Beyond/dp/0321227255).

Once you understand MPL, you will be able to develop highly optimized programs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top