【问题标题】:Compile error on boost::mpl::tag<T>::typeboost::mpl::tag<T>::type 上的编译错误
【发布时间】:2013-05-27 07:30:42
【问题描述】:

我最近尝试了 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>

//使用元函数标签获取类型,使得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<<',';} 
    }
};

这里是错误信息:

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

错误:需要一个类型,得到 'boost::mpl::tag::type'

【问题讨论】:

    标签: c++ boost boost-mpl


    【解决方案1】:

    你应该使用

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

    因为type 是dependent-name。阅读更多相关信息here

    【讨论】:

      【解决方案2】:

      不幸的是,您混淆了编译时和运行时编程的概念。

      在模板元编程世界中,您可以使用 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,您将能够开发高度优化的程序。

      【讨论】:

      • 其实有时候runtime if (some constexr)比纯元编程更合适。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-01-09
      • 1970-01-01
      • 2021-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多