我正在重写一个库,其中“接口(interface)”之一是全局数字变量(不要问)。
我将其替换为 UDT 的全局对象,该对象的作用类似于数字,支持 operator int() const
我的库的一些内部消费者将 std::max 与该全局变量一起使用。他们会得到编译错误,因为 std::max 只需要一个 T。
例如:
PseudoInt x; // in library
// in consumer
void conumser()
{
int y = std::max(5, x); // will error now, because int and PseudoInt are not the same T
}
我最希望我的消费者不必使用react,比如 std::max<int>(5, x)
。
我是不是运气不好?
最佳答案
由于 std::max
是一个模板,因此您可以使用 PseudoInt 和 Int 为 std::max
提供专门化。
关于c++ - std::min/std::max 为 "intrinsic-like",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37211286/