c++ - visual studio 中的神秘警告 (4180)

标签 c++ visual-studio function-pointers

1>c:\program files\microsoft visual studio 10.0\vc\include\map(229): warning C4180: 
     qualifier applied to function type has no meaning; ignored
1>          d:\...\gmproject.h(122) 
            : see reference to class template instantiation 'std::map<_Kty,_Ty>' 
            being compiled
1>          with
1>          [
1>              _Kty=GMProject::DuplicateTy,
1>              _Ty=GMProject::DuplicateFn
1>          ]

我的类有这些 typedef(pTree 是一个容器):

typedef void *DuplicateFn(pTree&, const pTree&); 
enum DuplicateTy {
    SKIP,
    OVERWRITE,
    ASK
};

typedef std::map<DuplicateTy, DuplicateFn> DuplicateMapTy;

第 122,123 行是:

static const DuplicateMapTy DuplicateFns;
static DuplicateMapTy DuplicateFns_INIT();

如何指示此映射不能更改 - 并使其对类保持静态? 我的目标是创建一个映射,以便我可以从枚举中“获取”一个函数指针。 (客户端代码将提供枚举,然后类本身将枚举解析为函数)。

最佳答案

这个问题与 map 是 const 无关:这是一个警告,因为 std::map::at() 的 const 版本的返回类型是 const mapped_type&。此代码同样会产生警告:

typedef void *DuplicateFn();

typedef std::map< int, DuplicateFn > DuplicateMapTy;

DuplicateMapTy DuplicateFns;

这里 map 的at的retrun类型是

const DuplicateFn&

虽然这个警告有它的位置(虽然我不太确定在这种特殊情况下它是否符合标准),但在这种情况下,对于使用 map 的代码在本地禁用它应该没有害处,或者如果你不喜欢 pragma 麻烦,将你的函数指针包装到一个简单的结构中。

编辑 正如 Gorpik 在下面的评论中指出的那样,这实际上是在该特定位置生成的,尽管未使用该函数。似乎 VS 编译器在寻找警告时确实有点激进:它确实考虑了声明。

template< class T >
struct CheckMe
{
  const T& at() //warning C4180 pops up
  {
    //gets not instantiated hence no error for missing returntype
  }
};

CheckMe< DuplicateFn > check;

关于c++ - visual studio 中的神秘警告 (4180),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8255659/

相关文章:

c++ - 具有显式数组与非显式数组的 glBufferData

方法链的 C++ lambda 表达式

c# - Visual Studio 2015 诊断工具不支持当前调试配置

c# - Visual Studio 错误 : "The debugger cannot unwind to this frame." on any exceptions

windows - 如何在 Visual Studio C++ 项目中指定 "any Windows SDK version greater than 10.0"?

C:将任何函数传递给函数

c++ - 用户空间中的内存障碍? (Linux,x86-64)

c++ - 将参数传递给另一个可变参数函数

c - 函数指针 - 将参数传递给函数指针

c - void* 函数指针数组转换