Inconsistency between std::string and string literals

后端 未结 6 1473
耶瑟儿~
耶瑟儿~ 2020-12-28 12:30

I have discovered a disturbing inconsistency between std::string and string literals in C++0x:

#include 
#include          


        
6条回答
  •  清歌不尽
    2020-12-28 13:07

    If we overloaded std::begin() and std::end() for const char arrays to return one less than the size of the array, then the following code would output 4 instead of the expected 5:

    #include 
    
    int main()
    {
        const char s[5] = {'h', 'e', 'l', 'l', 'o'};
        int i = 0;
        for (auto e : s)
            ++i;
        std::cout << "Number of elements: " << i << '\n';
    }
    

提交回复
热议问题