我正在使用一个数据结构,它希望使用 STLlimits
来确定我给它的结构的最小值、最大值和无穷大(我认为只有这些)值。我正在使用 Visual C++ 2010,如果有这些东西的具体实现细节。
这是我的数据类型的基本结构,PseudoTuple::ReturnWrapper
是需要限制支持的类型:
struct PseudoTuple
{
struct ReturnWrapper
{
//wraps return values for comparisons and such
}
typedef ReturnWrapper value_type;
//basic "tuple" implementation as a data front
}
借助复制和粘贴的强大功能,我numeric_limits
为它创建了一个小类,只实现了我认为需要的 3 个功能。返回值当前是临时的,只是为了看看这是否会编译。
namespace std
{
template<> class _CRTIMP2_PURE numeric_limits<PseudoTuple::ReturnWrapper>
: public _Num_int_base
{
public:
typedef PseudoTuple::ReturnWrapper _Ty;
static _Ty (__CRTDECL min)() _THROW0()
{ // return minimum value
return PseudoTuple::ReturnWrapper();
}
static _Ty (__CRTDECL max)() _THROW0()
{ // return maximum value
return PseudoTuple::ReturnWrapper();
}
static _Ty __CRTDECL infinity() _THROW0()
{ // return positive infinity
return PseudoTuple::ReturnWrapper();
}
};
}
#include <data structure using PseudoTuple>
我在此之后包含标题以确保它可以获取这些声明。我在这里遇到错误:
namespace detail {
template<typename coordinate_type, bool is_integer, bool has_infinity>
struct coordinate_limits_impl;
template<typename coordinate_type>
struct coordinate_limits_impl<coordinate_type, true, false> {
static const coordinate_type highest() {
return std::numeric_limits<coordinate_type>::max(); // --- error here ---
}
static const coordinate_type lowest() {
return std::numeric_limits<coordinate_type>::min();
}
};
//lots of stuff
}
coordinate_type
是一个 typedef PseudoTuple::ReturnWrapper
。这是错误:
error C2589: '(' : illegal token on right side of '::'
error C2059: syntax error : '::'
min
并且在and的所有用法上也收到了这个有趣的警告,这个警告max
与错误在同一行:
warning C4003: not enough actual parameters for macro 'max'
当我将此数据结构与std::array
of一起使用时std::string
,我仍然会收到这些警告,但不会弹出编译器错误。在这种情况下它也可以正常运行,所以整个事情必须以某种方式工作。max
但是在使用我的 custom 时它无法识别该功能numeric_limits
。
如果我将 更改max()
为infinity()
,它可以正常编译并继续在min()
. 这些名字给它一些悲伤min
,max
但我不知道为什么。不过,这确实告诉我它可以infinity
从我的实现中获取方法numeric_limits
。
编辑:删除了显示正确类型的数据结构代码正在传入,似乎无关紧要。
编辑 2:Mark B 解决了问题,但弹出了一个新问题:
error LNK2019: unresolved external symbol "__declspec(dllimport) public: static struct PseudoTuple::ReturnWrapper __cdecl std::numeric_limits<struct PseudoTuple::ReturnWrapper>::max(void)" (__imp_?max@?$numeric_limits@UReturnWrapper@PseudoTuple@@@std@@SA?AUReturnWrapper@PseudoTuple@@XZ) referenced in function "public: static struct PseudoTuple::ReturnWrapper const __cdecl kd_v1_0_8::spatial::detail::coordinate_limits_impl<struct PseudoTuple::ReturnWrapper,1,0>::highest(void)" (?highest@?$coordinate_limits_impl@UReturnWrapper@PseudoTuple@@$00$0A@@detail@spatial@kd_v1_0_8@@SA?BUReturnWrapper@PseudoTuple@@XZ)
所以有些东西弄乱了链接......得到相同的min
但不是infinity
.