我继承了一个必须从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()
重载。我已经尝试了一些解决方法,比如直接定义Convert函数,但这样我就遇到了链接器错误。
有人可以指出我错过了什么吗?使用旧平台工具集编译它没有问题。微软是否改变了新的东西?
我已经对转换器标头进行了沙盒化,并将其缩小。标记未注释的行无法编译,并抛出之前提到的编译错误(C2064)。
这是简化的Convert.hpp:
#pragma once
#include <string>
#include "Declarations.hpp"
#include "utfcpp\utf8.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;
}
}
}
如果有兴趣的话,这里是Declarations.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:link。
我仍然对建议感兴趣,但我95%肯定wchar_t
是杯子。
答案 0 :(得分:0)
我想你可能错过了一个模板&lt;&gt;在某个地方...
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(); }