默认模板参数中的MPL占位符替换

时间:2012-10-24 08:27:27

标签: c++ template-meta-programming boost-mpl

我在理解MPL占位符方面遇到了一些麻烦 有人可以解释一下为什么这段代码无法编译?

我希望数字0,1和& 2要打印,但是当编译器尝试确定Wrapper的默认模板参数的类型时,占位符似乎不会被实际类型替换。

#include <iostream>
#include <boost/mpl/inherit.hpp>
#include <boost/mpl/inherit_linearly.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/vector.hpp>

template <typename T> struct Traits;

template<> struct Traits<int>  { typedef boost::mpl::int_<0> type; };
template<> struct Traits<char> { typedef boost::mpl::int_<1> type; };
template<> struct Traits<bool> { typedef boost::mpl::int_<2> type; };

template <typename T, typename Type=typename Traits<T>::type > struct Wrapper
{
    Wrapper() { std::cout << "Value: " << Type::value << std::endl; }

    T value;
};

int main()
{
    typedef boost::mpl::inherit_linearly<
                boost::mpl::vector<int, char, bool>,
                boost::mpl::inherit<boost::mpl::_1, Wrapper<boost::mpl::_2> > >::type Object;

    Object obj;

    return 0;
}

这是gcc-4.1.2的错误(我知道......工作中的旧编译器)

# g++4 -I ../boost test.cpp -o test
test.cpp: In function 'int main()':
test.cpp:24: error: invalid use of undefined type 'struct Traits<mpl_::arg<2> >'
test.cpp:7: error: declaration of 'struct Traits<mpl_::arg<2> >'
test.cpp:24: error: template argument 2 is invalid
test.cpp:24: error: template argument 2 is invalid
test.cpp:24: error: template argument 2 is invalid
test.cpp:24: error: expected initializer before 'Object'
test.cpp:26: error: 'Object' was not declared in this scope
test.cpp:26: error: expected `;' before 'obj'

修改 在下面的Acorbe answer之后,我已经对我的示例程序做了一个变体,以说明为什么他提出的解决方案不符合我的需求。这澄清了我的追求。在这种情况下,我希望打印文本TYPE_A,TYPE_B,TYPE_A。 g ++错误是一样的。

#include <iostream>
#include <boost/mpl/inherit.hpp>
#include <boost/mpl/inherit_linearly.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/vector.hpp>

enum WrapperType { TYPE_A, TYPE_B };

template <typename T> struct Traits;

template<> struct Traits<int>  { typedef boost::mpl::int_<TYPE_A> type; };
template<> struct Traits<char> { typedef boost::mpl::int_<TYPE_B> type; };
template<> struct Traits<bool> { typedef boost::mpl::int_<TYPE_A> type; };

template <typename T, typename Type=typename Traits<T>::type > struct Wrapper;

template <typename T>
struct Wrapper<T, boost::mpl::int_<TYPE_A> >
{
    Wrapper() : value (0) { std::cout << "TYPE_A" << std::endl; }

    T value;
};

template <typename T>
struct Wrapper<T, boost::mpl::int_<TYPE_B> >
{
    Wrapper() { std::cout << "TYPE_B" << std::endl; }

    T value;
};

int main()
{
    typedef boost::mpl::inherit_linearly<
                boost::mpl::vector<int, char, bool>,
                boost::mpl::inherit<boost::mpl::_1, Wrapper<boost::mpl::_2> > >::type Object;

    Object obj;

    return 0;
}

这是gcc-4.5.4的错误

# g++ -I ../boost test.cpp -o test
test.cpp: In functie ‘int main()’:
test.cpp:37:79: fout: invalid use of incomplete type ‘struct Traits<mpl_::arg<2> >’
test.cpp:9:34: fout: declaration of ‘struct Traits<mpl_::arg<2> >’
test.cpp:37:79: fout: template argument 2 is invalid
test.cpp:37:81: fout: template argument 2 is invalid
test.cpp:37:83: fout: template argument 2 is invalid
test.cpp:37:91: fout: expected initializer before ‘Object’
test.cpp:39:9: fout: ‘Object’ was not declared in this scope
test.cpp:39:16: fout: expected ‘;’ before ‘obj’

这是(部分)来自clang ++ - 3.1的错误:

test.cpp:15:50: error: implicit instantiation of undefined template 'Traits<mpl_::arg<2> >'
    template <typename T, typename Type=typename Traits<T>::type > struct Wrapper;
                                                 ^
test.cpp:37:57: note: in instantiation of default argument for 'Wrapper<mpl_::arg<2> >' required here
                    boost::mpl::inherit<boost::mpl::_1, Wrapper<boost::mpl::_2> > >::type Object;
                                                        ^~~~~~~~~~~~~~~~~~~~~~~
test.cpp:9:34: note: template is declared here
    template <typename T> struct Traits;

2 个答案:

答案 0 :(得分:1)

我对您的代码进行了一些更改,将默认模板参数引发的typedef拆分为两个更简单的typedef

考虑一下:

template < typename T , typename Super_Type = Traits<T> > struct Wrapper
{ 
     typedef typename Super_Type::type Type;
     Wrapper() { std::cout << "Value: " << Type::value << std::endl; }

     T value;
};

这种方式可以编译。我的怀疑是你的原始表达(虽然正确)某种程度太复杂而无法被编译器正确扩展。

答案 1 :(得分:1)

好吧......我好像已经破解了...... 通常情况下,额外的抽象级别可以解决这个问题。

我创建了一个元函数类(genWrapper),它与占位符一起传递给mpl :: apply1。然后,那里的元函数将返回预期的包装类型。

以下是生成的程序(第2版) 感谢大家的帮助和指示。

#include <iostream>
#include <boost/mpl/apply.hpp>
#include <boost/mpl/inherit.hpp>
#include <boost/mpl/inherit_linearly.hpp>
#include <boost/mpl/int.hpp>
#include <boost/mpl/vector.hpp>

enum WrapperType { TYPE_A, TYPE_B };

template <typename T> struct Traits;

template<> struct Traits<int>  { typedef boost::mpl::int_<TYPE_A> type; };
template<> struct Traits<char> { typedef boost::mpl::int_<TYPE_B> type; };
template<> struct Traits<bool> { typedef boost::mpl::int_<TYPE_A> type; };

template <typename T, typename Type = typename Traits<T>::type> struct Wrapper;

template <typename T>
struct Wrapper<T, boost::mpl::int_<TYPE_A> >
{
    Wrapper() : value (0) { std::cout << "TYPE_A" << std::endl; }

    T value;
};

template <typename T>
struct Wrapper<T, boost::mpl::int_<TYPE_B> >
{
    Wrapper() { std::cout << "TYPE_B" << std::endl; }

    T value;
};

struct genWrapper
{
    template <typename T>
    struct apply
    {
        typedef Wrapper<T> type;
    };
};

int main()
{
    typedef boost::mpl::inherit_linearly<
                boost::mpl::vector<int, char, bool>,
                boost::mpl::inherit<boost::mpl::_1, boost::mpl::apply1<genWrapper, boost::mpl::_2> > >::type Object;

    Object obj;

    return 0;
}

这是结果输出:

# g++4 -I ../boost test.cpp -o test
# ./test
TYPE_A
TYPE_B
TYPE_A