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

标签 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;
};

所以,我想要实现的是:

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::push_back将元素添加到序列中。现在您要连接两个序列。我更改了 type<>::value 的类型至 mpl::char_ , 然后更改了 mpl::push_back 的顺序参数(首先是序列,然后是元素)。此外,您必须使用 push_front , 不是 push_back在这段代码中。最后,我添加了一个 ::typepush_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;
};

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

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

(打印 ic )。

关于c++编译时字符串连接使用boost-mpl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6037059/

相关文章:

c++ - 确定 (c++) 迭代器是否反向

c++ - 复制构造函数和基类 C++

c++ - 为arm-linux-gnueabi-gcc交叉编译libcurl

php - javascript子串提取

java - 正则表达式通过使用运算符(!,和,(,),|)拆分所有字符串的开始和结束位置来获取字符串数组在Java中

templates - Mercurial 模板 : separator for tags and bookmarks

c++ - 为什么我们可以将取消引用的指针递增到 C++ 中的常量数据?

c++ - 成员函数中的静态变量可以是线程安全的吗?

ios - segue 没有在 segue 中传输字符串(swift4)

C++ 用 SFINAE 重载泛型函数