我在理解 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 的回答之后,我制作了我的示例程序的变体,以说明为什么他提出的解决方案不符合我的需求。这澄清了我所追求的。在这种情况下,我希望打印文本 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;