|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 Croper 于 2019-12-6 06:08 编辑
今天翻c++标准库的时候,看到一段代码:
- template<class _Ty>
- class numeric_limits
- : public _Num_base
- { // numeric limits for arbitrary type _Ty (say little or nothing)
- public:
- _NODISCARD static constexpr _Ty(min)() noexcept //min作为函数名,单独加了括号
- { // return minimum value
- return (_Ty());
- }
- _NODISCARD static constexpr _Ty(max)() noexcept //max作为函数名,单独加了括号
- { // return maximum value
- return (_Ty());
- }
- _NODISCARD static constexpr _Ty lowest() noexcept //lowest作为函数名,没有加括号
- { // return most negative value
- return (_Ty());
- }
- _NODISCARD static constexpr _Ty epsilon() noexcept
- { // return smallest effective increment from 1.0
- return (_Ty());
- }
- ...
- };
复制代码 (出自limits)
发现numeric_limits的成员函数min和max都单独加上了括号,而其他的成员函数比如lowest等并没有,
而且其他代码,在引用它的时候,也带上了括号:
- template<class _Rep>
- _INLINE_VAR constexpr bool treat_as_floating_point_v = treat_as_floating_point<_Rep>::value;
- // STRUCT TEMPLATE duration_values
- template<class _Rep>
- struct duration_values
- { // gets arithmetic properties of a type
- _NODISCARD static constexpr _Rep zero() noexcept
- { // get zero value
- return (_Rep(0));
- }
- _NODISCARD static constexpr _Rep(min)() noexcept
- { // get smallest value
- return (numeric_limits<_Rep>::lowest()); //调用lowest的时候,无括号
- }
- _NODISCARD static constexpr _Rep(max)() noexcept
- { // get largest value
- return ((numeric_limits<_Rep>::max)()); //调用max的时候,有括号
- }
- };
复制代码 (出自chrono)
最先我的想法是,是不是跟min,max宏太常用有关系。但是函数名加不加括号完全不影响宏的调用。
然后又试了一下:
- struct t {
- t() {
- cout << "hello world" << endl;
- }
- };
- int main() {
- numeric_limits<t>::max();
- (numeric_limits<t>::max)();
- system("pause");
- }
复制代码
完全没看出两种调用方法的区别,
所以有人知道函数名加括号到底有什么意义么? |
|