我正在尝试使用boost-mpl在编译时连接字符串,但是收到了来自gcc的错误。这是样例代码 -
所以,我想要实现的是:
问题是在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和Andy的建议之后,代码确实可以编译,但是当我尝试打印结果时……
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’
mpl::front_inserter
。 - Diego Sevillatypedef typename mpl::copy< typename converter<B,C,D>::value, mpl::back_inserter< typename type<A>::value > >::type value;
。这将把 type<A>::value 中的所有字符添加到转换器字符串的末尾。 - Diego Sevilla