lmi
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lmi] Third-millennium GOTW#1: is <int> special? [Was: Default values fo


From: Greg Chicares
Subject: [lmi] Third-millennium GOTW#1: is <int> special? [Was: Default values for default arguments]
Date: Sat, 4 Feb 2017 19:12:23 +0000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Icedove/45.6.0

On 2017-02-04 18:22, Vadim Zeitlin wrote:
> On Sat, 4 Feb 2017 17:23:19 +0000 Greg Chicares <address@hidden> wrote:
> 
> GC> On 2017-02-04 14:59, Vadim Zeitlin wrote:
> ..
> GC> >  Personally I'm a bit wary of everything involving 
> std::initializer_list in
> GC> > C++, it's too simple to write something that compiles without any 
> warnings
> GC> > but doesn't behave like you thought it would with it (of course, the 
> worst
> GC> > offender here is the notorious std::vector<int> ctor, but there are 
> other
> GC>                                              ^^^
> GC> I believe you mean std::vector<bool>.
> 
>  No, I really meant std::vector<int> and the reason why it's so special is
> explained at https://herbsutter.com/2013/05/09/gotw-1-solution/ (see the
> answer for the question 2).

How odd--I was just re-reading that article again yesterday, and I didn't
think he was suggesting that <int> is special. I think it's "{}" that is
special, not <int>, because otherwise replacing <int> as in the first
pair of statements below with <double> as in the second pair would cause
their size()s to differ:

---------8<--------8<--------8<--------8<--------8<--------8<--------8<-------
diff --git a/sandbox_test.cpp b/sandbox_test.cpp
index 401b86d..00fdbb8 100644
--- a/sandbox_test.cpp
+++ b/sandbox_test.cpp
@@ -23,8 +23,35 @@
 
 #include "test_tools.hpp"
 
+#include <vector>
+
 int test_main(int, char*[])
 {
+    std::vector<int> i0{5, 7};           // size 2
+    std::vector<int> i1(5, 7);           // size 5
+
+    std::vector<double> d0{5, 7};        // size 2
+    std::vector<double> d1(5, 7);        // size 5
+    std::vector<double> d2(5.0, 7.0);    // size 5
+    std::vector<double> d3(5.1L, 7.0);   // size 5
+
+    std::vector<unsigned char> u0{5, 7}; // size 2
+    std::vector<unsigned char> u1(5, 7); // size 5
+
+    std::cout
+        << i0.size() << " i0.size()\n"
+        << i1.size() << " i1.size()\n"
+        << "\n"
+        << d0.size() << " d0.size()\n"
+        << d1.size() << " d1.size()\n"
+        << d2.size() << " d2.size()\n"
+        << d3.size() << " d3.size()\n"
+        << "\n"
+        << u0.size() << " u0.size()\n"
+        << u1.size() << " u1.size()\n"
+        << std::endl
+        ;
+
     return 0;
 }
 
--------->8-------->8-------->8-------->8-------->8-------->8-------->8-------

/opt/lmi/src/lmi[0]$make $coefficiency unit_tests 
unit_test_targets=sandbox_test.exe             
make[2]: Nothing to be done for 'build_unit_tests'.
List of unit-test targets that did not build successfully:

List ends.

Running sandbox_test:
2 i0.size()
5 i1.size()

2 d0.size()
5 d1.size()
5 d2.size()
5 d3.size()

2 u0.size()
5 u1.size()




reply via email to

[Prev in Thread] Current Thread [Next in Thread]