Why does including an arbitrary STL header fix these compilation errors?

I have this global function in my program:

static bool IsValidType(const CString& cType)
{
    for (auto pType : {"bmp","jpg","jpeg","gif","tif","tiff","png"})
        if (cType == CString(pType))
            return true;
    return false;
}

And it gives me the following compilation errors:

error C3312: no callable 'begin' function found for type 'initializer-list'
error C3312: no callable 'end' function found for type 'initializer-list'
error C2065: 'pType' : undeclared identifier

I can resolve it by including an arbitrary STL header in front of the function body, for example:

#include <string>
static bool IsValidType(const CString& cType)
{
    ...
}

But of course, I don’t think this is the right way to do this.

Can you explain to me why the inclusion of an arbitrary STL header fixes this problem and how should I solve it correctly?

Thank.

+4
source share
3 answers

Since you are using initializer_list , you must include initializer_list.

#include <initializer_list>

string , string, , initializer_list, include .

+8

, STL ,

.

?

, /.
<iterator>, <initializer_list> .

+4

STL.

std::begin std::end , : ( [iterator.container]/p1):

1 In addition to including the header <iterator>, function templates in 27.8 is available if the any of the following headers: <array>, <deque>, <forward_list>, <list>, <map>, <regex>, <set>, <string>, <unordered_map>, <unordered_set>and <vector>.

Oddly enough, <string_view>not on this list.

  • An existing defect report is reported here.
  • libC ++ at least defines std::beginand std::endif you included <string_view>.
+1
source

Source: https://habr.com/ru/post/1683607/


All Articles