使用boost-mpl进行c ++编译时字符串连接

Cod*_*eak 5 c++ string templates string-concatenation boost-mpl

我正在尝试使用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;
};
Run Code Online (Sandbox Code Playgroud)

所以,我想要实现的是:

converter<int,char,int> == "ici\0" // true. 
Run Code Online (Sandbox Code Playgroud)

问题是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’
Run Code Online (Sandbox Code Playgroud)

任何人都可以用上面的代码指出问题并解释正确的方法吗?谢谢

编辑1:更正格式和几个拼写错误

编辑2:在Lambdageek之后,Andy的建议代码确实编译但是当我尝试打印结果时

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

,编译器抱怨 -

/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
Run Code Online (Sandbox Code Playgroud)

我承认我对模板编程很新,所以我确定问题必须是基本的.谢谢你的帮助

编辑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’
Run Code Online (Sandbox Code Playgroud)

Die*_*lla 4

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

首先,您可以使用mpl::push_back向序列添加元素。现在您正在连接两个序列。type<>::value我更改了to的类型mpl::char_,然后更改了参数的顺序mpl::push_back(首先是序列,然后是元素)。另外,您必须在这段代码中使用push_front, 而不是。push_back最后,我::type在 后面添加了push_front,因为您必须在此处提取实际类型。这是供参考的代码:

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;
};
Run Code Online (Sandbox Code Playgroud)

现在这段代码可以按预期工作:

int
main (void)
{
        cout << mpl::c_str< converter<int,char>::value >::value << endl;
        return 0;
}
Run Code Online (Sandbox Code Playgroud)

(印刷ic)。

  • @Code:好的,可以做到。类型必须是:`typedef typename mpl::copy&lt; typename converter&lt;B,C,D&gt;::value, mpl::back_inserter&lt; typename type&lt;A&gt;::value &gt;&gt;::type value;`。这会将 type&lt;A&gt;::value 中的所有字符添加到转换器字符串的后面。 (2认同)