std::numeric_limits::max()问题,请大虾帮忙

colebin 2005-09-17 07:26:49
我有这样一个模板类:

#include <limits>
#include <list>

using namespace std;

template<typename T>
class HandleManager
{
public:
HandleManager(T);
HandleManager(T, size_t);
T provide();
void free(T);

private:
size_t maximum ;
T highest ;
std::list<T> available ;
};

/** Constructor. The default maximal value will be based on the type max.
@param init Initial handle value (use for first requested handle)
*/
template<typename T>
HandleManager<T>::HandleManager(T init)
: highest(init) //,maximum(std::numeric_limits<T>::max()) //注释掉
{
maximum = std::numeric_limits<T>::max();
}

// ============================================================================
/** Constructor
@param init Initial handle value (use for first requested handle)
@param hmax Maximal handle value
*/
template<typename T>
HandleManager<T>::HandleManager(T init, size_t hmax)
: highest(init), maximum(hmax) { }

// ----------------------------------------------------------------------------
/** Get a new handle
@return handle
@throw RTIinternalError if all handles between first and maximal are used
*/
template<typename T> T
HandleManager<T>::provide()
{
T handle = 0 ;

if (available.size() > 0) {
handle = available.front();
available.pop_front();
}
else {
if (highest < maximum)
handle = highest++ ;
}

return handle ;
}

// ----------------------------------------------------------------------------
/** Free a handle
@pre handle is a previously-provided handle
@param handle Handle to free
*/
template<typename T> void
HandleManager<T>::free(T handle)
{
if (handle + 1 == highest)
--highest ;
else
available.push_back(handle);
}

}


然后在一个类里调用 HandleManager<unsigned long> MyHandle;

但编译时出现:
warning C4003: “max”宏的实参不足
error C2589: “(” : “::”右边的非法标记
D:\Program Files\Microsoft Visual Studio .NET 2003\Vc7\include\xmemory(156) : 编译类模板成员函数“open::HandleManager<T>::HandleManager(T)”时
with
[
T=open::Handle
]
e:\工作空间\open source\RootObject.h(77) : 参见对正在编译的类模板实例化“open::HandleManager<T>”的引用
with
[
T=open::Handle
]

错误发生在maximum = std::numeric_limits<T>::max(); 语句中,请问这是什么原因 ?
...全文
1349 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
masterz 2005-09-18
  • 打赏
  • 举报
回复 2
maximum = (std::numeric_limits<T>::max)(); //把max用括号括起来避免和windows定义的宏混淆
colebin 2005-09-18
  • 打赏
  • 举报
回复
但我把std::numeric_limits<T>::max();注释掉后,编译就没问题啊
colebin 2005-09-18
  • 打赏
  • 举报
回复
masterz(www.fruitfruit.com) 回答正确,厉害,给分
fangrk 2005-09-17
  • 打赏
  • 举报
回复
应该不是std::numeric_limits<T>::max(); 的问题,下列的测试代码通过了bcb6和.net2003的编译
#include <iostream>
#include <limits>
using namespace std;

template<class T>
class TEST
{
public:
TEST():s(std::numeric_limits<T>::max()){}
private:
const size_t s;
};

int main()
{
TEST<int> T1;
TEST<unsigned long> T2;
}

就算外包起来也没有问题,比如:
#include <iostream>
#include <limits>
using namespace std;

template<class T>
class TEST
{
public:
TEST(T):s(std::numeric_limits<T>::max()){}
void Show() const{cout<<s<<'\n';}
private:
const size_t s;
};
template<class T>
class Wrap
{
public:
Wrap(T t):MM(t){}
TEST<T> MM;
};
int main()
{
TEST<int> T1(1);
T1.Show();
Wrap<unsigned long> W(3);
W.MM.Show();
}

64,636

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧