我的程序中有这样一个全局函数:
static bool IsValidType(const CString& cType)
{
for (auto pType : {"bmp","jpg","jpeg","gif","tif","tiff","png"})
if (cType == CString(pType))
return true;
return false;
}它给出了以下编译错误:
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我可以通过在函数体之前包含一个任意的STL头来解决它,例如:
#include <string>
static bool IsValidType(const CString& cType)
{
...
}当然,我不认为这是正确的做法。
你能给我解释一下为什么包含一个任意的STL头可以解决这个问题吗?我应该如何正确地解决这个问题呢?
谢谢。
发布于 2017-08-13 18:46:57
由于您使用的是initializer_list,因此应该包含initializer_list。
#include <initializer_list>包含string解决了错误,因为string可能包含initializer_list,但这种间接包含不是推荐的方式。
发布于 2017-08-13 18:45:58
你能给我解释一下为什么包含一个任意的STL头可以解决这个问题吗?
因为许多标准标头在其实现中包含其他标头。
以及我应该如何正确地解决它?
包括专用于包含这些缺少的函数/类型的标头。
在你的例子中,这就是<iterator>,根据documentation的<initializer_list>头。
发布于 2017-08-15 06:41:22
不是任意的STL标头。
如果你在标准中查看std::begin和std::end的定义,它是这样写的:(在iterator.container/p1中):
1除了通过包含
<iterator>标头可用外,还可以在包含以下任何标头时使用27.8中的函数模板:<array>、<deque>、<forward_list>、<list>、<map>、<regex>、<set>、d11、d12、d13和d14。
有趣的是,<string_view>不在该列表中。
如果包括<string_view>.,
<string_view>.的existing defect report报告定义了std::begin和std::end
https://stackoverflow.com/questions/45659489
复制相似问题