我试图在编译时使用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之后,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’发布于 2011-05-18 06:15:32
好的,根据你的最终编辑,我发现这里有几个问题。
首先,可以使用mpl::push_back将元素添加到序列中。现在,您正在连接两个序列。我将type<>::value的类型更改为mpl::char_,然后更改了序列参数的顺序(首先是mpl::push_back,然后是元素)。此外,在此代码中必须使用push_front,而不是push_back。最后,我在push_front之后添加了一个::type,因为您必须在这里提取实际的类型。下面是供参考的代码:
using namespace boost;
using namespace std;
template<class A>
struct type {};
template<>
struct type<int> {
typedef mpl::char_ < 'i' > value;
};
template<>
struct type<char> {
typedef mpl::char_ < '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 typename mpl::push_front< typename converter<B,C,D>::value, typename type<A>::value >::type value ;
};
template<>
struct converter<empty, empty, empty, empty> {
typedef mpl::string < '\0' > value;
};现在,这段代码可以正常工作了:
int
main (void)
{
cout << mpl::c_str< converter<int,char>::value >::value << endl;
return 0;
}(打印ic)。
发布于 2011-05-18 05:07:04
添加typename关键字来告诉编译器::value是一种类型是否有帮助?
struct converter {
typedef mpl::push_back< typename type<A>::value, typename converter<B,C,D>::value > value ;
};发布于 2011-05-18 05:08:49
您需要使用typename关键字:
typedef mpl::push_back< typename type<A>::value, typename converter<B,C,D>>:value >::type value;当访问使用模板参数实例化的模板的嵌套类型定义时,您需要帮助C++确定嵌套名称是引用方法/字段还是嵌套类型定义。如果你什么也没说,C++会假定它是一个字段名。如果你说typename,它会假设嵌套的东西是一个类型。
https://stackoverflow.com/questions/6037059
复制相似问题