// DEPRECATED: Please use std::numeric_limits (from <limits>) instead.
--
--
Chromium Developers mailing list: chromi...@chromium.org
View archives, change email options, or unsubscribe:
http://groups.google.com/a/chromium.org/group/chromium-dev
I'm seeing INTN_MAX being used even for local variables. For example:I just find it strange that base/basictypes.h explicitly recommends std::numeric_limits<>() while remaining silent on INTN_MAX.
I'm seeing INTN_MAX being used even for local variables. For example:I just find it strange that base/basictypes.h explicitly recommends std::numeric_limits<>() while remaining silent on INTN_MAX.
I'm seeing INTN_MAX being used even for local variables. For example:I just find it strange that base/basictypes.h explicitly recommends std::numeric_limits<>() while remaining silent on INTN_MAX.
If you're writing new code, prefer stdint.h, because it's now available and consistently so.
If you're dealing with existing code, arguably you should look to convert the types to the stdint.h types at some point, as well as the stdint.h macros.In the mean time, it might help to clarify that comment more, or perhaps just directly recommend the INTN_MAX macros as our custom typedefs are now gone and they're just wrappers for the stdint.h types.
I'm not sure why the availability of stdint.h automatically implies that we should prefer the stdint.h INTx_MAX family of macros over the things in numeric_limits? It just means those are widely available to use now if people want them.
On Tue, Aug 18, 2015 at 5:06 PM, Peter Kasting <pkas...@chromium.org> wrote:I'm not sure why the availability of stdint.h automatically implies that we should prefer the stdint.h INTx_MAX family of macros over the things in numeric_limits? It just means those are widely available to use now if people want them.See Nico's reply. numeric_limits<> is not a guaranteed to be compiler-optimized,
and comes with a static ctor overhead.
I'm not sure what you mean by "those are widely available to use now if people want to them" - both the INTx_MAX macros and numeric_limits<> are widely available,
but the INTx_MAX macros have reliable performance characteristics across all of the Chromium compilers, while numeric_limits<> has a variety of subtleties.
Assuming the above is correct, "causes a static initializer if used in a global" (which numeric_limits values should rarely be used for) seems a bit more minor than "a variety of subtleties" makes it sound.
std::numeric_limits<>() causes a static initializer when used in globals for now
std::numeric_limits<>() causes a static initializer when used in globals for nowI don't get that. Why are globals different than local variables?
$ cat test.cc#include <limits>int max_val = std::numeric_limits<int>::max();int foo() {return std::numeric_limits<int>::max();}$ gcc -O3 -S test.cc -o- | c++filt.file "test.cc".text.p2align 4,,15.globl foo().type foo(), @functionfoo():.LFB152:.cfi_startprocmovl $2147483647, %eaxret.cfi_endproc.LFE152:.size foo(), .-foo().section .text.startup,"ax",@progbits.p2align 4,,15.type _GLOBAL__sub_I_max_val, @function_GLOBAL__sub_I_max_val:.LFB154:.cfi_startprocmovl $2147483647, max_val(%rip)ret.cfi_endproc.LFE154:.size _GLOBAL__sub_I_max_val, .-_GLOBAL__sub_I_max_val.section .init_array,"aw".align 8.quad _GLOBAL__sub_I_max_val.globl max_val.bss.align 16.type max_val, @object.size max_val, 4max_val:.zero 4.ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4".section .note.GNU-stack,"",@progbits$ gcc -std=c++11 -O3 -S test.cc -o- | c++filt.file "test.cc".text.p2align 4,,15.globl foo().type foo(), @functionfoo():.LFB171:.cfi_startprocmovl $2147483647, %eaxret.cfi_endproc.LFE171:.size foo(), .-foo().globl max_val.data.align 4.type max_val, @object.size max_val, 4max_val:.ident "GCC: (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4".section .note.GNU-stack,"",@progbits
Or is this just that we don't care (that much) about initializers for locals?
Since you're writing "std::numeric_limits<>()" with brackets at the end, does it make a difference whether the constructor is called or not? On Linux, both of the following compiles to a constant expression:int a = std::numeric_limits<int>::max();int b = std::numeric_limits<int>().max();
Thanks a lot for the detailed explanation! This is very interesting.Is this a GCC-only problem?
std::numeric_limits<>() causes a static initializer when used in globals for nowI don't get that. Why are globals different than local variables?
My original question was more a matter of coding style and whether there's a style preference in Chrome for one or the other. It sounds like while a case could be made for one or the other, there is no official style preference. Personally I am more comfortable using INTx_MAX because it's less syntactically cumbersome than numeric_limits<> and doesn't require an extra include.However, the comment in base/basictypes.h gives the impression that there is a coding style requirement to use numeric_limits instead of INTx_MAX. If that is indeed not the case (i.e. could go either way), we should update the comment to reflect both options.