4
#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;
};

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

错误信息: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]
        ]
4

5 回答 5

4

string-literal 不是指针,array of N const char在您的情况下,它是 ,因为boost::any构造函数接收T(推导为char[5],而不是const char*,数组到指针的转换在这里不起作用),但是您不能通过initializer-list.

于 2013-11-13T09:25:13.157 回答
2

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

boost::any charArr = +"haha"; 

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

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

于 2013-11-13T10:30:34.733 回答
2

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

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

于 2013-11-13T09:36:39.940 回答
1

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

1>D:\SRC\CDR\Trunk\DRIT\ThirdParty\boost/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:\SRC\CDR\Trunk\DRIT\ThirdParty\boost/any.hpp(139) : see declaration of 'boost::any::holder<ValueType>::held'
1>          with
1>          [
1>              ValueType=const char [5]
1>          ]
1>          D:\SRC\CDR\Trunk\DRIT\ThirdParty\boost/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:\SRC\CDR\Trunk\DRIT\ThirdParty\boost/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>          ]

“哈哈”的类型,不是aconst char*而是a const char[5]。如果将字符串转换为 achar*这将编译:

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

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

是讨论在 Boost 中添加数组支持的链接。

于 2013-11-13T09:35:47.420 回答
0

此问题的简化版本:

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;
}
于 2013-11-13T14:27:44.573 回答