【问题标题】:boost::mpl::string size error messagesboost::mpl::string 大小错误消息
【发布时间】:2016-06-03 06:05:00
【问题描述】:

这是我正在处理的用例的较小版本。

#ifndef BOOST_MPL_LIMIT_STRING_SIZE
# define BOOST_MPL_LIMIT_STRING_SIZE 64
#endif

#include <boost/mpl/string.hpp>
#include <iostream>

using str = boost::mpl::string<'a','b','c','d','e','f','.','g','h','i','j','k','l','m','n','.','o','p','q','r','s','t','u','v','w','x','y','z'>;

int main()
{
    std::cout<<"String is "<<boost::mpl::c_str<str>::value<<std::endl;
}

使用 gcc 4.9.3 给了我以下错误消息。

source2.cpp:19:143: error: wrong number of template arguments (28, should be 16)
 using str = boost::mpl::string<'a','b','c','d','e','f','.','g','h','i','j','k','l','m','n','.','o','p','q','r','s','t','u','v','w','x','y','z'>;
                                                                                                                                               ^
In file included from source2.cpp:16:0:
/boost/include/boost/mpl/string.hpp:135:12: error: provided for ‘template<int C0, int C1, int C2, int C3, int C4, int C5, int C6, int C7, int C8, int C9, int C10, int C11, int C12, int C13, int C14, int C15> struct boost::mpl::string’
     struct string;
            ^
source2.cpp: In function ‘int main()’:
source2.cpp:23:48: error: ‘str’ was not declared in this scope
     std::cout<<"String is "<<boost::mpl::c_str<str>::value<<std::endl;
                                                ^
source2.cpp:23:51: error: template argument 1 is invalid
     std::cout<<"String is "<<boost::mpl::c_str<str>::value<<std::endl;

我已经咨询了一堆与boost::mpl::string 相关的问题,但并没有解决我的问题。

为什么即使我已经覆盖了大小,编译器也会给我错误消息?

我是否遗漏了一些非常明显的东西?

【问题讨论】:

    标签: c++ c++11 boost boost-mpl


    【解决方案1】:

    您必须将其作为多字符常量传递,并且不能超过 4 个字节。

    #include <boost/mpl/string.hpp>
    #include <iostream>
    
    
    using str1 = boost::mpl::string<'abcd','ef.g','hijk','lmn.','opqr','stuv','wxyz'>;
    
    int main()
    {
        std::cout<<"String is "<<boost::mpl::c_str<str1>::value<<std::endl;
    }
    

    更新:: 考虑到每个长度为 4 的模板参数,使用宏 BOOST_MPL_LIMIT_STRING_SIZE。因此,在您的情况下,预期的字符串大小为 number of template arguments * 4。因此,128 应该是一个不错的值。

    【讨论】:

    • 看来我什至不能有 1、2 或 3 个字节,即 aababc 将不起作用。它必须是abcd。 :(
    • @Recker 你可以,最多 4 个字节。当我使用“ab”或“abc”时,我没有收到任何错误。它只是模板参数的数量除以四时不应大于 BOOST_MPL_LIMIT_STRING_SIZE
    • 不。在 clang 编译器 3.8 上我没有运气。我正在尝试将 BOOST_MPL_LIMIT_STRING_SIZE 设置为 128 的 &lt;'a','ab','abc','abcd'&gt;。但它无法编译。 error : multi-character character constant.
    • 你必须在编译时添加'-Wmultichar'标志。
    猜你喜欢
    • 2011-05-03
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-04
    相关资源
    最近更新 更多