首页 文章

boost :: mpl :: for_each的类型包装器错误(Abrahams&Gurtovoy书中的第9.1.1节)

提问于
浏览
2

以下代码几乎逐字逐句地复制自David Abrahams和Aleksey Gurtovoy撰写的C Template Metaprogramming:概念,工具和技术来自Boost and Beyond的第9.1.1节 .

唯一的变化是我希望能够使用常规的Boost模板mpl :: identity从书中更改类型包装器模板 . 但是,在Microsoft Visual C Express 2010(SP1)下,如果我这样做,我会收到一个神秘的编译器警告 .

似乎某种程度上与类型包装器模板具有名为“type”的内部typedef这一事实有关 . 将typedef更改为“Type”(或简单地删除该行)将使代码正常工作 . 有没有人对这种奇怪的行为有解释?

#include <iostream>
#include <typeinfo>
#include <boost/mpl/for_each.hpp>
#include <boost/mpl/vector.hpp>

namespace mpl = boost::mpl;

// added a nested typedef named "type" is equivalent to mpl::identity
template <class T>
struct wrap 
{
    // changing type -> Type or removing this line 
    // makes the code compile and produce correct output!
    typedef T type; 
};

struct print_type
{
    template <class T>
    void operator() (wrap<T>) const
    {
        std::cout << typeid(T).name() << std::endl;
    }
};

class A
{
    A() {}  // private constructor
};

class B
{
    B() {}  // private constructor
};

typedef boost::mpl::vector<A,B> AB;

int main()
{
    boost::mpl::for_each<AB, wrap<mpl::_> >(
        print_type()
    );

    /* Output:
    class A
    class B
    */

    return 0;
}

输出 /I"C:\Program Files\boost\boost_1_47" /I"C:\Program Files\boost" /Zi /nologo /W3 /WX- /O2 /Oi /Oy- /GL /D "WIN32" /D "NDEBUG" /D "_CONSOLE" /D "_UNICODE" /D "UNICODE" /Gm- /EHsc /GS /Gy /fp:precise /Zc:wchar_t /Zc:forScope /Fp"Release\mpl.pch" /Fa"Release\" /Fo"Release\" /Fd"Release\vc100.pdb" /Gd /analyze- /errorReport:queue

1>------ Build started: Project: mpl, Configuration: Release Win32 ------
1>  main.cpp
1>C:\Program Files\boost\boost_1_47\boost/mpl/for_each.hpp(75): error C2784: 'void print_type::operator ()(wrap<T>) const' : could not deduce template argument for 'wrap<T>' from 'arg'
1>          ..\..\..\mpl\main.cpp(20) : see declaration of 'print_type::operator ()'
1>          C:\Program Files\boost\boost_1_47\boost/mpl/for_each.hpp(101) : see reference to function template instantiation 'void boost::mpl::aux::for_each_impl<false>::execute<first,last,TransformOp,F>(Iterator *,LastIterator *,TransformFunc *,F)' being compiled
1>          with
1>          [
1>              TransformOp=wrap<boost::mpl::_>,
1>              F=print_type,
1>              Iterator=first,
1>              LastIterator=last,
1>              TransformFunc=wrap<boost::mpl::_>
1>          ]
1>          ..\..\..\mpl\main.cpp(42) : see reference to function template instantiation 'void boost::mpl::for_each<AB,wrap<T>,print_type>(F,Sequence *,TransformOp *)' being compiled
1>          with
1>          [
1>              T=boost::mpl::_,
1>              F=print_type,
1>              Sequence=AB,
1>              TransformOp=wrap<boost::mpl::_>
1>          ]
========== Build: 0 succeeded, 1 failed, 2 up-to-date, 0 skipped ==========

1 回答

  • 2

    一些术语,从Boost.MPL reference manual转述:

    • 元函数是一种具有名为 type 的嵌套类型的类型

    • 元函数类是一个带有嵌套元函数的类型,名为 apply

    • 占位符表达式是一种类型,它是MPL占位符或类模板特化,至少有一个参数本身是占位符表达式

    因此,我们知道 wrap<> 是一个元函数, wrap<mpl::_> 既是占位符表达式又是元函数 .

    mpl::for_each<> 传递 TransformOp 模板参数的元函数或元函数类时,它会评估该元函数/元函数类以获取转换结果 . 因此,如果要在不进一步评估的情况下传递原始占位符表达式,则占位符表达式必须不符合元函数或元函数类的条件 .

    在您的场景中,因为 wrap<> 是一个元函数, mpl::for_each<> 正在评估它并生成 AB 作为转换类型;同时, print_type::operator()<> 期待 wrap<A>wrap<B> - 当然这将拒绝编译 .

相关问题