Domanda

I'm using a data structure that wants to use STL limits to determine the min, max, and infinity (I think only these) values of a struct I'm giving it. I'm using Visual C++ 2010, if there is implementation specific details for this stuff.

Here's the basic structure of my data types, with PseudoTuple::ReturnWrapper being the type needing limits support:

struct PseudoTuple
{
 struct ReturnWrapper
 {
  //wraps return values for comparisons and such
 }

 typedef ReturnWrapper value_type;

 //basic "tuple" implementation as a data front
}

With the power of copy and paste, I created a small numeric_limits class for it, implementing only the 3 functions I think are required. The return values are currently temporary, just to see if this will compile.

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>

And I include the header after this to make sure it can grab these declarations. I'm getting an error in it here:

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 is a typedef of PseudoTuple::ReturnWrapper. Here's the error:

error C2589: '(' : illegal token on right side of '::'
error C2059: syntax error : '::'

And also getting this interesting warning on all uses of min and max, this one being on the same line as the error:

warning C4003: not enough actual parameters for macro 'max'

When I use this data structure with an std::array of std::string, I still get those warnings but no compiler errors pop up. It also runs properly in that case, so the whole thing must work somehow. But it doesn't recognize the max function when using my custom numeric_limits.

If I change the max() to infinity(), it compiles fine and moves on to throwing an error on min(). The names min and max are giving it some grief, but I'm not sure why. This does tell me that it can grab the infinity method from my implementation of numeric_limits, though.

edit: Removed data structure code showing the right type is being passed in, seems irrelevant.

edit 2: Mark B solved the problem, but a new one popped up:

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)

So something is messing up the linking... Getting the same for min but not for infinity.

È stato utile?

Soluzione

Sometimes min and max were implemented as macros which just substitute text. It's not your specialization that's causing issues here but the macros. I believe windows.h is one such offender. You can use #define NOMINMAX to cause them to not be created as macros.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top