C++ c++;使用boostmpl的编译时字符串连接

C++ c++;使用boostmpl的编译时字符串连接,c++,string,templates,string-concatenation,boost-mpl,C++,String,Templates,String Concatenation,Boost Mpl,我试图在编译时使用boostmpl连接字符串,但从gcc得到错误。这是样品- using namespace boost; using namespace std; template<class A> struct type {}; template<> struct type<int> { typedef mpl::string < 'i' > value; }; template<> struct type<ch

我试图在编译时使用boostmpl连接字符串,但从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;
};
使用名称空间boost;
使用名称空间std;
模板
结构类型{};
模板
结构类型{
typedef mpl::字符串<'i'>值;
};
模板
结构类型{
typedef mpl::字符串<'c'>值;
};
结构为空{
};
模板
结构转换器;
模板
结构转换器{
typedef mpl::push_back::type value;
};
模板
结构转换器{
typedef mpl::字符串<'\0'>值;
};
所以,我想要实现的是:

converter<int,char,int> == "ici\0" // true. 
converter==“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’
main.cpp:37:错误:“模板结构boost::mpl::push_back”的模板参数列表中参数1的类型/值不匹配
main.cpp:37:错误:应为类型,但得到“type::value”
main.cpp:37:错误:“模板结构boost::mpl::push_back”的模板参数列表中参数2的类型/值不匹配
main.cpp:37:错误:应为类型,获取“converter::value”
有人能指出上面代码的问题并解释正确的方法吗?谢谢

编辑1:更正格式和少数打字错误

编辑2: 在Lambdageek之后,Andy建议代码可以编译,但是当我试图打印结果时

int main(int argc, char** argv) {
    cout << mpl::c_str< converter<int,char>::value >::value << endl;
    return 0;
}
int main(int argc,char**argv){

cout::value添加
typename
关键字是否有助于告诉编译器
::value
是一个类型

struct converter {
    typedef mpl::push_back< typename type<A>::value, typename converter<B,C,D>::value > value ;
};
struct转换器{
typedef mpl::push_backvalue;
};

您需要使用
typename
关键字:

typedef mpl::push_back< typename type<A>::value, typename converter<B,C,D>>:value >::type value;
typedefmpl::push_back:value>::type value;
当访问使用模板参数实例化的模板的嵌套typedef时,您可以 需要帮助C++决定嵌套名称是否引用方法/字段或嵌套类型定义。 如果你不说什么,C++会假设它是一个字段名。如果你说“代码>类型名称< /Cord>”,它将假定
嵌套的东西是一种类型。

好的,根据您最后的编辑,我在这里看到了几个问题

首先,您可以使用
mpl::push_
向序列中添加元素。现在您将连接两个序列。我将
type::value
的类型更改为
mpl::char
,然后更改
mpl::push_
参数的顺序(首先是序列,然后是元素)。此外,您必须在此代码中使用
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;
};
使用名称空间boost;
使用名称空间std;
模板
结构类型{};
模板
结构类型{
typedef mpl::char<'i'>值;
};
模板
结构类型{
typedef mpl::char<'c'>值;
};
结构为空{
};
模板
结构转换器;
模板
结构转换器{
typedef typename mpl::push_front::type value;
};
模板
结构转换器{
typedef mpl::字符串<'\0'>值;
};
现在,此代码按预期工作:

int
main (void)
{
        cout << mpl::c_str< converter<int,char>::value >::value << endl;
        return 0;
}
int
主(空)
{

cout::value谢谢。您的建议有效,但当我尝试打印值时出现新错误,请检查对问题的编辑。尝试将
::type
添加到
push_back
行。请参见将该行更改为typedef mpl::push_back:type value中的示例。但现在我得到了更多信息n、 cpp:41:错误:类型“boost::mpl::push_back”不是从类型“converter”派生出来的@Code:键盘有问题。“you”总是通过一个“u”来表示。还有,大写的“I”似乎坏了。你可能想买一个新键盘。谢谢。请检查更新后的问题。代码现在可以编译了,但我在添加代码打印结果时再次出错。这很有效。非常感谢。但好奇的是,有没有办法使用mpl::string而不是char_uz来完成同样的操作?另外,我投了赞成票,但无法将其标记为原始答案lambdageek已经回答了所有问题-对不起that@Code,您的问题的答案不会生成有效的代码:)无论如何,我将研究如何在所有位置使用mpl::string,可能使用
mpl::front\u插入器
@code:OK,可以这样做。类型必须是:
typedef typename mpl::copy>::type value;
。这将把type::value中的所有字符添加到转换器字符串的后面。谢谢。看起来很复杂,但应该尝试一下:)
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;
}