gpt4 book ai didi

c++ - 删除模板参数的 g++ 警告

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:25:48 25 4
gpt4 key购买 nike

我有一个简单的类:

template<size_t N, typename T>
class Int
{
bool valid(size_t index) { return index >= N; }
T t;
}

如果我将此类的实例定义为:

Int<0, Widget> zero;

我收到 g++ 警告:

warning: comparison is always true due to limited range of data type

我尝试这样做,但我无法弄清楚如何使用非类型模板参数部分特化一个函数。看起来可能无法在 g++ 中禁用此警告。隐藏此警告或编写此方法使其在 N==0 时始终返回 true 的正确方法是什么?

谢谢!

最佳答案

所以,我想出了以下解决方案:

template<size_t N>
bool GreaterThanOrEqual(size_t index)
{
return index >= N;
}

template<>
bool GreaterThanOrEqual<0l>(size_t index)
{
return true;
}

所以现在,类看起来像:

template<size_t N, typename T>
class Int
{
bool valid(size_t index) { return GreaterThanOrEqual<N>(index); }
T t;
}

当然,我收到一个未使用的参数警告,但有办法解决这个问题......

这是一个合理的解决方案吗?

关于c++ - 删除模板参数的 g++ 警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4630805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com