以下代码使用VC 15.3.5编译得很好:
#include <Windows.h>
#include <string>
#include <memory>
#include <optional>
#include <functional>
class TypeDictionary;
class IStructureField {};
using FieldFactory = std::function<std::unique_ptr<IStructureField>(TypeDictionary& dict, LPCSTR szName, std::optional<ULONG> ulOffset, std::optional<std::string> strFieldType, std::optional<ULONG> ulFieldSize)>;
int main()
{
FieldFactory ff;
ff = [](TypeDictionary& dict, LPCSTR szName, std::optional<ULONG> ulOffset, std::optional<std::string> szFieldType, std::optional<ULONG> ulFieldSize) -> std::unique_ptr<IStructureField> {
return std::unique_ptr<IStructureField>();
};
return 0;
}
它使用15.4.0生成以下错误消息:
1>f:\projects\fullpath\fullpath\fullpath.cpp(22): error C2679: binary '=': no operator found which takes a right-hand operand of type 'main::<lambda_36489dce1cc59e5a8de4329a29806b5a>' (or there is no acceptable conversion)
1>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.11.25503\include\functional(656): note: could be 'std::function<std::unique_ptr<IStructureField,std::default_delete<_Ty>> (TypeDictionary &,LPCSTR,std::optional<ULONG>,std::optional<std::string>,std::optional<ULONG>)> &std::function<std::unique_ptr<_Ty,std::default_delete<_Ty>> (TypeDictionary &,LPCSTR,std::optional<ULONG>,std::optional<std::string>,std::optional<ULONG>)>::operator =(std::nullptr_t) noexcept'
1> with
1> [
1> _Ty=IStructureField
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.11.25503\include\functional(629): note: or 'std::function<std::unique_ptr<IStructureField,std::default_delete<_Ty>> (TypeDictionary &,LPCSTR,std::optional<ULONG>,std::optional<std::string>,std::optional<ULONG>)> &std::function<std::unique_ptr<_Ty,std::default_delete<_Ty>> (TypeDictionary &,LPCSTR,std::optional<ULONG>,std::optional<std::string>,std::optional<ULONG>)>::operator =(std::function<std::unique_ptr<_Ty,std::default_delete<_Ty>> (TypeDictionary &,LPCSTR,std::optional<ULONG>,std::optional<std::string>,std::optional<ULONG>)> &&)'
1> with
1> [
1> _Ty=IStructureField
1> ]
1>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.11.25503\include\functional(612): note: or 'std::function<std::unique_ptr<IStructureField,std::default_delete<_Ty>> (TypeDictionary &,LPCSTR,std::optional<ULONG>,std::optional<std::string>,std::optional<ULONG>)> &std::function<std::unique_ptr<_Ty,std::default_delete<_Ty>> (TypeDictionary &,LPCSTR,std::optional<ULONG>,std::optional<std::string>,std::optional<ULONG>)>::operator =(const std::function<std::unique_ptr<_Ty,std::default_delete<_Ty>> (TypeDictionary &,LPCSTR,std::optional<ULONG>,std::optional<std::string>,std::optional<ULONG>)> &)'
1> with
1> [
1> _Ty=IStructureField
1> ]
1>f:\projects\fullpath\fullpath\fullpath.cpp(22): note: while trying to match the argument list '(FieldFactory, main::<lambda_36489dce1cc59e5a8de4329a29806b5a>)'
1>Done building project "FullPath.vcxproj" -- FAILED.
正如我在cppcon上看到的那样(斯蒂芬的演讲),<functional>
已经完成了很多工作......这有关系吗?我犯了什么错误?
解决方法也没问题。
Edit1:添加了两个包含文件,以帮助理解问题不存在:同样的错误...
Edit2:修复我的VS安装后,代码刚编译完毕。但是,原始代码没有。之前我曾遇到https://developercommunity.visualstudio.com/content/problem/74313/compiler-error-in-xsmf-control-with-stdoptional-pa.html并试图解决这个问题(这暴露了我面临的问题)。没有解决方法,代码编译(但再次点击__this问题......)。