简体   繁体   English

为什么 MVS 编译器可以将参数“myStruct”转换为“myStruct &”。 并且没有提交错误 C2664:无法将“myStruct”转换为“myStruct &”

[英]Why the MVS compiler can convert argument 'myStruct' to 'myStruct &'. And did not file error C2664: cannot convert 'myStruct' to 'myStruct &'

I have a class where I use std::mem_fn to choose between helper functions.我有一个类,我使用std::mem_fn在辅助函数之间进行选择。

  1. Why my code get compiled and run If I am missing & in m_funcContainer deceleration ?为什么我的代码被编译并运行如果我在m_funcContainer deceleration 中丢失& In the code & commented out with /**/ myStruct/*&*/在代码&使用已经注释掉/**/ myStruct/*&*/

std::map < std::string, std::function<void(const myClass*, myStruct/*&*/) >> m_funcContainer

(but in case of m_funcContainerInt the compiler rise compile error) (但在m_funcContainerInt的情况下,编译器会引发编译错误)

error C2664: 'void (int &) const' : cannot convert argument 1 from 'int' to 'int &'错误 C2664:“void (int &) const”:无法将参数 1 从“int”转换为“int &”

  1. I feel that I have not formulate the title of my question in a best way, can you please help me to formulate technically more correct title?我觉得我没有以最好的方式制定我的问题的标题,你能帮我制定技术上更正确的标题吗?

Why the compiler can convert argument 'myStruct' to 'myStruct &' in std::function为什么编译器可以在 std::function 中将参数 'myStruct' 转换为 'myStruct &'

My simplified code is我的简化代码是

myClass.h

 #include <memory> #include <map> #include <functional> struct ExtraFlag { }; struct Flag { }; struct myStruct { std::shared_ptr<ExtraFlag> extraFlag; std::shared_ptr<Flag> flag; explicit myStruct() { } }; class myClass { private: std::map < std::string, std::function<void(const myClass*, myStruct/*&*/) >> m_funcContainer; std::map < std::string, std::function<void(const myClass*, int/*&*/) >> m_funcContainerInt; private: void funcMyStruct(myStruct& arg1) const; void funcInt(int& arg1) const; public: myClass(); };

myClass.cpp

 #include "myClass.h" myClass::myClass() { m_funcContainer["func"] = std::mem_fn(&myClass::funcMyStruct); myStruct myStructInstance; m_funcContainer.at("func")(this, myStructInstance); int a; m_funcContainerInt["func"] = std::mem_fn(&myClass::funcInt); m_funcContainerInt.at("func")(this, a); } void myClass::funcMyStruct(myStruct& arg1) const {} void myClass::funcInt(int& arg1) const {}

EDITED I am compiling on Microsoft visual studio 2013编辑我正在 Microsoft Visual Studio 2013 上编译

Your problem is that MSVC2013 is not a C++ compiler under its default settings.您的问题是 MSVC2013 在其默认设置下不是 C++ 编译器。 It is compiles a language closely related to C++, but with "extensions".它编译了一种与 C++ 密切相关的语言,但带有“扩展”。 You are being bitten by one of them.你被其中之一咬了。

/Za will turn off (most?) language extensions, I believe including the one causing you a problem here. /Za将关闭(大多数?)语言扩展,我相信包括导致您出现问题的那个。

I have heard reports that some headers that ship with MSVC (system headers) can have problems with /Za .我听说过一些随 MSVC(系统标头)一起提供的标头可能会出现问题/Za And, code that was compiled and tested with /Za off could have unexpected behavior changes with /Za turned on.而且,这是编译和测试代码/Za关闭可能有意想不到的行为变化与/Za打开。 I would include it by default in new files or projects, and if you have an old project activate it and test that it doesn't cause problems.默认情况下,我会将它包含在新文件或项目中,如果您有一个旧项目,请激活它并测试它不会导致问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM