Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why boost::any does not hold string literal?

#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]
        ]
like image 354
Jichao Avatar asked Jan 12 '23 22:01

Jichao


2 Answers

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.

like image 167
ForEveR Avatar answered Jan 17 '23 16:01

ForEveR


Boost.any values must be valid to assign (requirement of ValueType). A string literal is however an array and arrays cannot be assigned in C++.

You can just cast the literal to a const char * if you need that.

like image 29
6502 Avatar answered Jan 17 '23 18:01

6502