C2440编译错误:__cplusplus编译选项引发的

413 阅读1分钟
  • error C2440: “=”: 无法从“_Ty”转换为“T”
// qhashfunctions.h
// like std::exchange
template <typename T, typename U = T>
Q_DECL_RELAXED_CONSTEXPR T qExchange(T &t, U &&newValue)
{
    T old = std::move(t);
    t = std::forward<U>(newValue); // here
    return old;
}
  • error C2440: “reinterpret_cast”: 无法从“int”转换为“quintptr”
// qglobal.h
Q_DECL_CONST_FUNCTION inline uint qHash(std::nullptr_t, uint seed = 0) noexcept
{
    return qHash(reinterpret_cast<quintptr>(nullptr), seed);// here
}

排查了好久,提示无法从int转换为qunitptr,可是nullptr怎么是int呢?

最终发现httpglobal.h发现有重定义nullptr,问题就出在这个为什么导致重定义上?

image.png

问题就出在这个__cplusplus上,查阅资料发现,这个是vs的历史遗留问题,增加编译选项/Zc:__cplusplus即可

image.png

CMake中增加编译选项:

if(MSVC)
    target_compile_options(${APP_NAME} PUBLIC "/Zc:__cplusplus")
endif()