Visual Studio 中的神秘警告 (4180)

发布于 2024-12-17 23:16:19 字数 951 浏览 1 评论 0原文

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();

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

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>          ]

Well my class has these typedefs (pTree is a container):

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

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

And lines 122,123 are:

static const DuplicateMapTy DuplicateFns;
static DuplicateMapTy DuplicateFns_INIT();

How do I indicate this map can't change - and makes it static to the class?
My goal is to create a map so I can "get" a function pointer from the enumerate. (The client code will provide the enum, then the class itself resolves the enum to a function).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

懵少女 2024-12-24 23:16:19

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

typedef void *DuplicateFn();

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

DuplicateMapTy DuplicateFns;

The retrun type of map's at here is

const DuplicateFn&

虽然此警告有它的位置(尽管我不太确定在这种特殊情况下它是否符合标准),在这种情况下,对于使用映射的代码在本地禁用它应该没有什么坏处,或者如果您不喜欢编译指示麻烦,请将函数指针包装到一个简单的结构中。

正如 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;

The issue has nothing to do with the map being const: it's a warning because the returntype of the const version of std::map::at() is const mapped_type&. This code produces the warning just as well:

typedef void *DuplicateFn();

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

DuplicateMapTy DuplicateFns;

The retrun type of map's at here is

const DuplicateFn&

While this warning has it's place (although I'm not too sure in this particular situation it is justified by the standard), in this case there should be no harm to disable it locally for the code using the map, or if you do not like the pragma hassle, wrap your function pointer into a simple struct.

edit as Gorpik points out in the comment below, this is actually generated at that specific location though the function isn't used. Seems indeed the VS compiler is kind of aggressive when hunting for warnings: it does consider declarations.

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

CheckMe< DuplicateFn > check;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文