std::map 给出类型转换错误,但仅在为 x86 编译时

std::map gives typeconversion error, but only when compiling for x86

使用 Visual Studio 2017,我有一段代码,我在其中定义了一个 std::map 在 x64 中编译正常但 returns 在为 x86 编译时出错。

有问题的地图键是一个枚举和 returns 一个带有描述符字符串和一些函数指针的结构。

我检查了我的项目设置并确认了两者之间我的 C++ 编译器设置的唯一区别是体系结构标志。我最好的猜测是它与每个映射条目中的函数指针有关,因为我在同一个文件中有其他 std::maps 包含字符串结构和 doubles/floats 都可以正常工作。

抛出的具体错误是C2440:Can't convert from type1 to type2,文本cannot convert from 'initializer list' to std::map<int,PropertyMetaData, std::less<_Kty>, std::allocator<std::pair<const _Kty,_Ty>>>

编辑:

我做了一个没有任何外部依赖的最紧凑的例子。我在 x64 中没有收到任何错误,但在设置为 x86 时出现类型转换错误:

#include <map>

typedef double(*convertFunc)(int, int, double);
typedef int(*convertFuncD)(int, int, double*,double*,int);

extern "C" {
    __declspec(dllexport) double __stdcall Pressure(
        int inUnits,
        int outUnits,
        double inValue
    );

    __declspec(dllexport) int __stdcall PressureD(
        int inUnits,
        int outUnits,
        double* inValue,
        double* outValue,
        int n
    );
}

//Metadata for each Property
struct PropertyMetaData {
    const char desc[20]; //Text description
    const convertFunc func; //double conversion function
    const convertFuncD funcArrayD; //array double conversion function
};

// Map containing all of the properties and their metadata
typedef std::map<int, PropertyMetaData> PropertiesMap;

const PropertiesMap conversions = {
    //Mapping The type of unit (mass, distance, etc.) to the appropriate function

    //Enumeration                   {desc[20],      func,        arrayfuncD,   arrayFuncF   }
    { 1,    {"Pressure",    Pressure,    PressureD}},
};

经过更多的试验,这似乎是由 __stdcall 限定符引起的。如果将其删除,我对这两种架构都没有问题。

知道了!问题是在函数声明中包含 __stdcall,或者更确切地说,在函数指针类型定义中省略了 __stdcall。似乎对于某些架构组合,如果 __stdcall 不在指针类型定义中,则 __stdcall 可能会或可能不会导致类型转换错误。声明我的函数指针类型如下:

typedef double(__stdcall *convertFunc)(EUnits, EUnits, double);
typedef int(__stdcall *convertFuncD)(EUnits, EUnits, double*,double*,int);
typedef int(__stdcall *convertFuncF)(EUnits, EUnits, float*, float*, int);

解决了错误!