C2064 从 MSVC v100 更新到 v140 平台工具集时出现编译错误

c2064 compilation error on updating from msvc v100 to v140 platform toolset

本文关键字:工具集 错误 编译 平台 v140 MSVC v100 更新 C2064      更新时间:2023-10-16

我继承了一个必须从 MSVC 平台工具集 v100 更新到 v140 的项目。它是为了即将发布的Archicad。一切都很好,除了当我将平台工具集设置为 140 时,我的一个模板函数变得疯狂并出现编译错误:

C2064 项的计算结果不等于采用 2 个参数的函数

在此之后,我收到了同一行的警告:

类没有定义"operator()"或用户定义的转换运算符,以指向函数的指针或引用到函数,该运算符采用适当数量的参数

此块中由 IDE 指向的 return 语句:

template<typename T, typename From>
inline T Convert(From from, const std::locale& locale)
{
converters::Converter<From, T> conv;
return conv(from, locale); // <- compilation fails on this line.
}

我有几个这样的专业:

CONVERTER(API_Guid, GS::Guid, from, locale)
{
(void)locale;    // It gets optimized away
return APIGuid2GSGuid(from);
}

CONVERTER定义为:

#define CONVERTER(From, T, from, locale) 
template<> 
struct Converter<From, T> : public std::true_type 
{ 
inline T operator()(const From& from, const std::locale& locale) const; 
} ;
T Converter<From, T>::operator()(const From& from, const std::locale& locale) const

转换器类的operator()重载。我已经尝试了一些解决方法,例如直接定义转换函数,但这样我就遇到了链接器错误。

有人可以指出我错过了什么吗?我使用较旧的平台工具集编译它没有问题。Microsoft在新的版本中改变了什么吗?


我已经对转换器标头进行了沙盒处理,并缩小了范围。标记了未注释无法编译的行,并抛出前面提到的编译错误 (C2064)。

这是减少的Convert.hpp:

#pragma once
#include <string>
#include "Declarations.hpp"
#include "utfcpputf8.h"
#define CONVERTER(From, T, from, locale) 
template<> 
struct Converter<From, T> : public std::true_type 
{ 
inline T operator()(const From& from, const std::locale& locale) const; 
} ;
T Converter<From, T>::operator()(const From& from, const std::locale& locale) const
namespace et
{
template<typename T>
inline T Convert(const wchar_t* str, const std::locale& locale = std::locale::classic())
{
std::wstring wstr(str);
return Convert<T>(wstr, locale);
}
template<typename T, typename From>
inline T Convert(From from, const std::locale& locale)
{
converters::Converter<From, T> conv;
return conv(from, locale);
}

template<typename From>
inline std::string S(const From& from)
{
return Convert<std::string>(from);
}

inline std::string S(const wchar_t* from)      
{
// return Convert<std::string>(from); <- This line fails to compile on V140, but does on V100
}
namespace converters
{
template<typename From, typename T, typename _Enabler1, typename _Enabler2, typename _Enabler3>
struct Converter : _FALSETYPE_
{
};
template<typename From, typename T>
struct Converter < From, T, _IF_CONVERTIBLE_(From, T) > : _TRUETYPE_
{
inline T operator()(const From& from, const std::locale&) const
{
return (T)from;
}
};
CONVERTER(std::string, bool, from, locale)
{
(void)locale;   // It gets optimized away
return et::Convert<bool>(from.c_str());
}

CONVERTER(bool, std::string, from, locale)
{
(void)locale;   // It gets optimized away
return from ? std::string("true") : std::string("false");
}
CONVERTER(std::string, et::UString, s, locale)
{
(void)locale;   // It gets optimized away
et::UString dest;
utf8::utf8to16(s.begin(), s.end(), std::back_inserter(dest));
return dest;
}
CONVERTER(et::UString, std::string, from, locale)
{
(void)locale;   // It gets optimized away
std::string dest;
utf8::utf16to8(from.begin(), from.end(), std::back_inserter(dest));
return dest;
}
}
}

如果有人感兴趣,这里是 声明.hpp 以便于复制:

#pragma once
#include <string>
#define _TRUETYPE_  public std::true_type
#define _FALSETYPE_ public std::false_type
#define _IF_CONVERTIBLE_(From, To)  typename std::enable_if<std::is_convertible<From, To>::value>::type
#define _IF_ARITHMETIC_(A)      typename std::enable_if<std::is_arithmetic<A>::value>::type
#define _IF_ARITHMETIC_T_(A, T) typename std::enable_if<std::is_arithmetic<A>::value, T>::type
namespace et
{
template<typename T, typename Enabler = void, typename Blah = void>
struct IsConvertibleToString : public std::false_type
{
};
template<typename T>
struct IsConvertibleToString<T, typename std::enable_if<std::is_convertible<T, std::string>::value>::type > : public std::true_type
{
};
typedef std::u16string UString;
template<typename T, typename From>
T Convert(From from, const std::locale& locale = std::locale::classic());
template<typename From, typename T>
struct ConvertFunctor;
namespace converters
{
template<typename From, typename To, typename _Enabler1 = void, typename _Enabler2 = void, typename _Enabler3 = void>
struct Converter;
}
}
#define _IF_STRING_CONVERTIBLE_(T) typename std::enable_if<::et::IsConvertibleToString<T>::value>::type

UTF-8 CPP 可以从这里获得:链接。

我仍然对建议感兴趣,但我 95% 确定wchar_t是 cuplrit。

我想你可能缺少一个模板<>某处...

template<>
struct Converter<From, Type> : public std::true_type
{
inline Type operator()(const From& from, const std::locale& locale) const;
};
// missing template<> here ??  As is, this is not ANSI compliant
Type Converter<From, T>::operator()(const From& from, const std::locale& locale) const { return Type(); }