代码之家  ›  专栏  ›  技术社区  ›  bobber205

每个问题的答案

  •  8
  • bobber205  · 技术社区  · 16 年前

    尝试将下面的内容与char数组一起使用,但无法编译。但是使用short[]的例子效果很好。知道为什么吗?:)

    char someChars[] = {'s','h','e','r','r','y'};
        BOOST_FOREACH(char& currentChar, someChars)
        {
    
        }
    
    
    short array_short[] = { 1, 2, 3 };
        BOOST_FOREACH( short & i, array_short )
        {
            ++i;
        }
    
    1 回复  |  直到 16 年前
        1
  •  17
  •   James McNellis    16 年前

    如果你去排队 <boost/foreach.hpp>

    // **** READ THIS IF YOUR COMPILE BREAKS HERE ****
    //
    // There is an ambiguity about how to iterate over arrays of char and wchar_t. 
    // Should the last array element be treated as a null terminator to be skipped, or
    // is it just like any other element in the array? To fix the problem, you must
    // say which behavior you want.
    //
    // To treat the container as a null-terminated string, merely cast it to a
    // char const *, as in BOOST_FOREACH( char ch, (char const *)"hello" ) ...
    //
    // To treat the container as an array, use boost::as_array() in <boost/range/as_array.hpp>,
    // as in BOOST_FOREACH( char ch, boost::as_array("hello") ) ...
    

    使用 boost::as_array(someChars) 如注释所示,应该修复编译错误。