我试图在编译时使用 boost-mpl 连接字符串,但我从 gcc 得到错误。这是样本 -
using namespace boost;
using namespace std;
template<class A>
struct type {};
template<>
struct type<int> {
typedef mpl::string < 'i' > value;
};
template<>
struct type<char> {
typedef mpl::string < 'c' > value;
};
struct empty {
};
template<class A, class B, class C, class D>
struct converter;
template<class A, class B = empty, class C = empty, class D = empty>
struct converter {
typedef mpl::push_back< type<A>::value, converter<B,C,D>::value >::type value ;
};
template<>
struct converter<empty, empty, empty, empty> {
typedef mpl::string < '\0' > value;
};
所以,我想要实现的是:
converter<int,char,int> == "ici\0" // true.
问题是 gcc 中的上述代码会引发以下错误:
main.cpp:37: error: type/value mismatch at argument 1 in template parameter list for ‘template<class Sequence, class T> struct boost::mpl::push_back’
main.cpp:37: error: expected a type, got ‘type::value’
main.cpp:37: error: type/value mismatch at argument 2 in template parameter list for ‘template<class Sequence, class T> struct boost::mpl::push_back’
main.cpp:37: error: expected a type, got ‘converter::value’
任何人都可以指出上述代码的问题并解释正确的方法吗?谢谢
编辑 1:更正了格式和一些错别字
编辑 2:在 Lambdageek 之后,安迪的建议代码确实可以编译,但是当我尝试打印结果时
int main(int argc, char** argv) {
cout << mpl::c_str< converter<int,char>::value >::value << endl;
return 0;
}
,编译器抱怨 -
/usr/local/include/boost/mpl/string.hpp:534: instantiated from ‘boost::mpl::c_str<boost::mpl::push_back<boost::mpl::string<105, 0, 0, 0, 0, 0, 0, 0>, boost::mpl::push_back<boost::mpl::string<105, 0, 0, 0, 0, 0, 0, 0>, boost::mpl::string<0, 0, 0, 0, 0, 0, 0, 0> > > >’
main.cpp:49: instantiated from here
/usr/local/include/boost/mpl/string.hpp:228: error: ‘value’ is not a member of ‘boost::mpl::push_back<boost::mpl::string<105, 0, 0, 0, 0, 0, 0, 0>, boost::mpl::string<0, 0, 0, 0, 0, 0, 0, 0> >’
/usr/local/include/boost/mpl/string.hpp: In instantiation of ‘boost::mpl::c_str<boost::mpl::push_back<boost::mpl::string<105, 0, 0, 0, 0, 0, 0, 0>, boost::mpl::push_back<boost::mpl::string<105, 0, 0, 0, 0, 0, 0, 0>, boost::mpl::string<0, 0, 0, 0, 0, 0, 0, 0> > > >’:
main.cpp:49: instantiated from here
/usr/local/include/boost/mpl/string.hpp:548: error: no type named ‘value_type’ in struct boost::mpl::push_back<boost::mpl::string<105, 0, 0, 0, 0, 0, 0, 0>, boost::mpl::push_back<boost::mpl::string<105, 0, 0, 0, 0, 0, 0, 0>, boost::mpl::string<0, 0, 0, 0, 0, 0, 0, 0> > >’
main.cpp: In function ‘int main(int, char**)’:
main.cpp:49: error: ‘value’ is not a member of ‘boost::mpl::c_str<boost::mpl::push_back<boost::mpl::string<105, 0, 0, 0, 0, 0, 0, 0>, boost::mpl::push_back<boost::mpl::string<105, 0, 0, 0, 0, 0, 0, 0>, boost::mpl::string<0, 0, 0, 0, 0, 0, 0, 0> > > >’
make[2]: *** [build/Debug/GNU-Linux-x86/main.o] Error 1
make[1]: *** [.build-conf] Error 2
我承认我对模板编程很陌生,所以我确信问题一定是基本的。感谢所有的帮助
编辑 3:更改了转换器结构中的 push_back 行。
错误:
main.cpp:41: error: type ‘boost::mpl::push_back<typename type<A>::value, typename converter<B, C, D, empty>::value>’ is not derived from type ‘converter<A, B, C, D>’
main.cpp:41: error: expected ‘;’ before ‘value’