为什么 boost::any 不保存字符串文字

Why boost::any does not hold string literal?

本文关键字:字符串 文字 保存 any boost 为什么      更新时间:2023-10-16
#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;
};

注释的代码行无法编译,为什么?我认为字符串文字在大多数情况下可以充当字符*,这是相关的 r 值和 l-rvalue 吗?

错误信息:test_boost_any.cc

D:Program Files (x86)Microsoft Visual Studio 11.0VCINCLUDExlocale(336) : wa
rning C4530: C++ exception handler used, but unwind semantics are not enabled. S
pecify /EHsc
e:projectsframeworkboost_1_53_0boost/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:projectsframeworkboost_1_53_0boost/any.hpp(139) : see declaration
of 'boost::any::holder<ValueType>::held'
        with
        [
            ValueType=const char [5]
        ]
        e:projectsframeworkboost_1_53_0boost/any.hpp(120) : while compiling
class template member function 'boost::any::holder<ValueType>::holder(ValueType
(&))'
        with
        [
            ValueType=const char [5]
        ]
        e:projectsframeworkboost_1_53_0boost/any.hpp(47) : see reference to
function template instantiation 'boost::any::holder<ValueType>::holder(ValueType
 (&))' being compiled
        with
        [
            ValueType=const char [5]
        ]
        e:projectsframeworkboost_1_53_0boost/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]
        ]

字符串文字不是指针,它是array of N const char,在您的情况下,因为boost::any构造函数接收T(推导为char[5],而不是const char*,数组到指针的转换在这里不起作用),但你不能用initializer-list中的另一个数组初始化一个数组。

Boost.any 值必须有效才能分配(要求 ValueType )。但是,字符串文本是一个数组,数组不能在C++中分配。

如果需要,您可以将文本转换为const char *

这里 C 中粗糙的数组语义最简单的解决方法是

boost::any charArr = +"haha"; 

请注意,使用 + 将 char 数组隐式衰减为const char*

其他人已经解释了数组值语义的问题

编译器会告诉你它不能接受数组,例如VS2010会告诉你:

1>D:SRCCDRTrunkDRITThirdPartyboost/any.hpp(122): error C2536: 'boost::any::holder<ValueType>::boost::any::holder<ValueType>::held' : cannot specify explicit initializer for arrays
1>          with
1>          [
1>              ValueType=const char [5]
1>          ]
1>          D:SRCCDRTrunkDRITThirdPartyboost/any.hpp(139) : see declaration of 'boost::any::holder<ValueType>::held'
1>          with
1>          [
1>              ValueType=const char [5]
1>          ]
1>          D:SRCCDRTrunkDRITThirdPartyboost/any.hpp(120) : while compiling class template member function 'boost::any::holder<ValueType>::holder(ValueType (&))'
1>          with
1>          [
1>              ValueType=const char [5]
1>          ]
1>          D:SRCCDRTrunkDRITThirdPartyboost/any.hpp(46) : see reference to class template instantiation 'boost::any::holder<ValueType>' being compiled
1>          with
1>          [
1>              ValueType=const char [5]
1>          ]
1>          toto.cpp(20) : see reference to function template instantiation 'boost::any::any<const char[5]>(ValueType (&))' being compiled
1>          with
1>          [
1>              ValueType=const char [5]
1>          ]

"哈哈"的类型,不是const char*而是const char[5].如果将字符串转换为char*这将编译:

boost::any charArr = static_cast<const char*>("haha"); // will compile

或者,例如,您可以只存储std::string。我怀疑这是因为数组不能存储为指针。您也可以使用boost::arraystd::array(如果有的话)。

下面是在 Boost 中添加阵列支持的讨论链接。

这个问题

的简化版本:

template <typename T>
class Array
{
    public:
        Array(T value) : value_(value) {}  
    private:
        T value_;
};
int main()
{
    int a[5] = {1,2,3,4,5};
    Array<int[5]> arr = a;
    return 0;
}