本文介绍了为什么提振::任何不抱字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 的#include<升压/ any.hpp>
#包括LT&;列表>
#包括LT&;串GT;
#包括LT&;矢量>结构_time_t {
    INT月;
    INT年;
};
诠释的main()
{
    性病::字符串str =hahastr;
    _time_t吨;
    的std ::矢量<提高::任何> objVec;
    objVec.push_back(1);
    字符* PSTR =哈哈;
    //提振::任何charArr =哈哈;不编译
    //objVec.push_back(\"haha);不编译
    objVec.push_back(PSTR);
    objVec.push_back(STR);
    objVec.push_back(T);
    返回0;
};

在评论code线不编译,为什么?我觉得字符串能够在大多数情况下为char *采取行动,这是相关的R值和L-右值?

错误信息:
test_boost_any.cc

  D:\\ Program Files文件(x86)的\\微软的Visual Studio 11.0 \\ VC \\ INCLUDE \\ xlocale(336):WA
rning C4530:C ++异常处理程序使用,但尚未启用放松语义。小号
pecify / EHSC
E:\\项目\\框架\\ boost_1_53_0 \\升压/ any.hpp(122):错误C2536:'的boost ::任何
::持有人<&值类型GT; ::提高::任何::持有人<&值类型GT; ::举办:不能指定expli
CIT初始化数组
        同
        [
            值类型=为const char [5]
        ]
        E:\\项目\\框架\\ boost_1_53_0 \\升压/ any.hpp(139):看到声明
的;值类型>的boost ::任何::持有者LT ::举办
        同
        [
            值类型=为const char [5]
        ]
        E:\\项目\\框架\\ boost_1_53_0 \\升压/ any.hpp(120):在编译
类模板成员函数的boost ::任何::持有人<&值类型GT; ::架(值类型
(安培))'
        同
        [
            值类型=为const char [5]
        ]
        E:\\项目\\框架\\ boost_1_53_0 \\升压/ any.hpp(47):见参考
函数模板实例化的boost ::任何::持有人<&值类型GT; ::架(值类型
 (安培))正在编制
        同
        [
            值类型=为const char [5]
        ]
        E:\\项目\\框架\\ boost_1_53_0 \\升压/ any.hpp(46):见参考
类模板实例化的boost ::任何::持有人<&值类型GT;'正在编译
        同
        [
            值类型=为const char [5]
        ]
        test_boost_any.cc(19):见参考函数模板实例
 提振::任何::任何<为const char [5]>(值类型(安培))正在编制
        同
        [
            值类型=为const char [5]
        ]


解决方案

字符串字面不是指针,它的ň为const char 的阵列,在你的情况下,由于的boost ::任何构造收到 T (被推断为的char [5] ,而不是为const char * ,数组到指针的转换不能在这里工作),但你不能用另一个数组在<$ C初始化数组$ C>初始化列表。

#include <boost/any.hpp>
#include <list>
#include <string>
#include <vector>

struct _time_t {
    int month;
    int year;
};


int main()
{
    std::string str = "hahastr";
    _time_t t;
    std::vector<boost::any> objVec;
    objVec.push_back(1);
    char* pstr = "haha";
    //boost::any charArr = "haha"; not compile
    //objVec.push_back("haha"); not compile
    objVec.push_back(pstr);
    objVec.push_back(str);
    objVec.push_back(t);
    return 0;
};

the commented code lines do not compile, why? I think string literal could act as char* in most circumstance, is this related r-value and l-rvalue?

error message:test_boost_any.cc

D:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\INCLUDE\xlocale(336) : wa
rning C4530: C++ exception handler used, but unwind semantics are not enabled. S
pecify /EHsc
e:\projects\framework\boost_1_53_0\boost/any.hpp(122) : error C2536: 'boost::any
::holder<ValueType>::boost::any::holder<ValueType>::held' : cannot specify expli
cit initializer for arrays
        with
        [
            ValueType=const char [5]
        ]
        e:\projects\framework\boost_1_53_0\boost/any.hpp(139) : see declaration
of 'boost::any::holder<ValueType>::held'
        with
        [
            ValueType=const char [5]
        ]
        e:\projects\framework\boost_1_53_0\boost/any.hpp(120) : while compiling
class template member function 'boost::any::holder<ValueType>::holder(ValueType
(&))'
        with
        [
            ValueType=const char [5]
        ]
        e:\projects\framework\boost_1_53_0\boost/any.hpp(47) : see reference to
function template instantiation 'boost::any::holder<ValueType>::holder(ValueType
 (&))' being compiled
        with
        [
            ValueType=const char [5]
        ]
        e:\projects\framework\boost_1_53_0\boost/any.hpp(46) : see reference to
class template instantiation 'boost::any::holder<ValueType>' being compiled
        with
        [
            ValueType=const char [5]
        ]
        test_boost_any.cc(19) : see reference to function template instantiation
 'boost::any::any<const char[5]>(ValueType (&))' being compiled
        with
        [
            ValueType=const char [5]
        ]
解决方案

string-literal is not a pointer, it's array of N const char, in your case, since boost::any constructor receive T (which is deduced to char[5], not to const char*, array-to-pointer conversion cannot work here), but you cannot initialize an array by another array in an initializer-list.

这篇关于为什么提振::任何不抱字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 05:17