[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [lmi] Third-millennium GOTW#1: is <int> special?
From: |
Vadim Zeitlin |
Subject: |
Re: [lmi] Third-millennium GOTW#1: is <int> special? |
Date: |
Sat, 4 Feb 2017 23:16:31 +0100 |
On Sat, 4 Feb 2017 19:12:23 +0000 Greg Chicares <address@hidden> wrote:
GC> On 2017-02-04 18:22, Vadim Zeitlin wrote:
GC> > On Sat, 4 Feb 2017 17:23:19 +0000 Greg Chicares <address@hidden> wrote:
GC> >
GC> > GC> On 2017-02-04 14:59, Vadim Zeitlin wrote:
GC> > ..
GC> > GC> > Personally I'm a bit wary of everything involving
std::initializer_list in
GC> > GC> > C++, it's too simple to write something that compiles without any
warnings
GC> > GC> > but doesn't behave like you thought it would with it (of course,
the worst
GC> > GC> > offender here is the notorious std::vector<int> ctor, but there are
other
GC> > GC> ^^^
GC> > GC> I believe you mean std::vector<bool>.
GC> >
GC> > No, I really meant std::vector<int> and the reason why it's so special is
GC> > explained at https://herbsutter.com/2013/05/09/gotw-1-solution/ (see the
GC> > answer for the question 2).
GC>
GC> How odd--I was just re-reading that article again yesterday, and I didn't
GC> think he was suggesting that <int> is special.
It's special in the sense that the same problem doesn't happen with e.g.
std::vector<std::string>. Of course, it also may happen with other
arithmetic types, although it's much more rare to run into it with them.
GC> I think it's "{}" that is special, not <int>,
Yes, it's "{}" which is special in the sense that it always takes
precedence over any other ctor (which, IMO, was a mistake), but it's
especially problematic with int.
GC>
---------8<--------8<--------8<--------8<--------8<--------8<--------8<-------
GC> diff --git a/sandbox_test.cpp b/sandbox_test.cpp
GC> index 401b86d..00fdbb8 100644
GC> --- a/sandbox_test.cpp
GC> +++ b/sandbox_test.cpp
GC> @@ -23,8 +23,35 @@
GC>
GC> #include "test_tools.hpp"
GC>
GC> +#include <vector>
GC> +
GC> int test_main(int, char*[])
GC> {
GC> + std::vector<int> i0{5, 7}; // size 2
GC> + std::vector<int> i1(5, 7); // size 5
This is the really horrible case.
GC> + std::vector<double> d0{5, 7}; // size 2
GC> + std::vector<double> d1(5, 7); // size 5
This is indeed similar, but it's less common as the variants with "7.0"
are used more often.
GC> + std::vector<double> d2(5.0, 7.0); // size 5
GC> + std::vector<double> d3(5.1L, 7.0); // size 5
And those should warn about implicit double-to-int conversion, so they're
not that bad.
GC> + std::vector<unsigned char> u0{5, 7}; // size 2
This is pretty bad too, but vector<char> is less common on its own. Also,
it only works with literal 7, but it wouldn't work with literal 777 (not a
big consolation, I know).
Regards,
VZ
- [lmi] Default values for default arguments, Greg Chicares, 2017/02/03
- Re: [lmi] Default values for default arguments, Vadim Zeitlin, 2017/02/04
- Re: [lmi] Default values for default arguments, Greg Chicares, 2017/02/04
- Re: [lmi] Default values for default arguments, Vadim Zeitlin, 2017/02/04
- [lmi] Third-millennium GOTW#1: is <int> special? [Was: Default values for default arguments], Greg Chicares, 2017/02/04
- Re: [lmi] Third-millennium GOTW#1: is <int> special?,
Vadim Zeitlin <=
- Re: [lmi] Third-millennium GOTW#1: is <int> special?, Greg Chicares, 2017/02/05
- Re: [lmi] Third-millennium GOTW#1: is <int> special?, Vadim Zeitlin, 2017/02/06
- [lmi] More enabled warnings, less boost [Was: Third-millennium GOTW#1: is <int> special?], Greg Chicares, 2017/02/06
- Re: [lmi] More enabled warnings, less boost, Vadim Zeitlin, 2017/02/06
- Re: [lmi] More enabled warnings, less boost, Greg Chicares, 2017/02/06
- Re: [lmi] More enabled warnings, less boost, Vadim Zeitlin, 2017/02/06
Re: [lmi] Default values for default arguments, Greg Chicares, 2017/02/10