在boost :: mpl :: tag <t> :: type </t>上编译错误

时间:2013-05-27 07:30:42

标签: c++ boost boost-mpl

我最近尝试过boost :: mpl,它看起来既棒棒又可怕。有时编译错误信息相当混乱。

这次我遇到以下代码的问题:

#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>

//使用元函数标签&lt;&gt;获取类型,以便mpl只输出整数。

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<<',';} 
    }
};

以下是错误消息:

错误:模板参数列表中参数1的类型/值不匹配'template struct boost :: is_same'

错误:期待一个类型,得到'boost :: mpl :: tag :: type'

2 个答案:

答案 0 :(得分:2)

你应该使用

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

因为typedependent-name。阅读有关它的更多信息here

答案 1 :(得分:1)

不幸的是,您正在混合编译时和运行时编程的概念。

在模板元编程世界中,您可以使用类似enable_if(http://en.cppreference.com/w/cpp/types/enable_if)来完成您想要的任务。

在该页面中,有许多示例将说明如何根据编译时类型选择在运行时执行的实现。

我还建议阅读Dave Abraham关于MPL的书(http://www.amazon.com/Template-Metaprogramming-Concepts-Techniques-Beyond/dp/0321227255)。

了解MPL后,您将能够开发高度优化的程序。

相关问题