lmi
[Top][All Lists]
Advanced

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

Re: [lmi] Cating doubles to enums


From: Greg Chicares
Subject: Re: [lmi] Cating doubles to enums
Date: Tue, 6 Nov 2018 13:49:23 +0000
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.0

On 2018-11-06 00:59, Vadim Zeitlin wrote:
> On Tue, 6 Nov 2018 00:36:17 +0000 Greg Chicares <address@hidden> wrote:
[...]
> GC> Most of the changes are clearly for the better IMO, but this one
> GC> bothers me:
> GC> 
> GC> -    oenum_alb_or_anb const alb_anb =
> GC> -        static_cast<oenum_alb_or_anb>
> GC> -            (database_->Query(DB_AgeLastOrNearest)
> GC> -            );
> GC> +    oenum_alb_or_anb alb_anb;
> GC> +    database_->query_into(alb_anb, DB_AgeLastOrNearest);
> GC> 
> GC> because there's no 'alb_anb' data member to assign into;
> 
>  Sorry but why is this a problem? I.e. why should query_into() be only used
> with members and not local variables?

Yes, it should be usable with local variables. My objections are that
  T t;
  query_into(t, db_key);
is two statements; that 't' can't be const here; and also that it's
nicer to define every variable at its point of declaration.

> GC> maybe
> GC> that suggests that such a member should exist, or maybe it means
> GC> I should reverse the order of the function template arguments
> GC> and default the "destination" argument:
> GC> 
> GC>  template<typename T>
> GC> -void product_database::query_into(T& dst, e_database_key k) const
> GC> +T product_database::query_into(e_database_key k, T& dst = T(0)) const
> 
>  This won't compile, will it? A non-const reference can't be bound to a
> temporary.

One could regard compiler warnings as insolent challenges to one's
programming acumen; thus:

---------8<--------8<--------8<--------8<--------8<--------8<--------8<-------
diff --git a/sandbox_test.cpp b/sandbox_test.cpp
index 0ed05aed..e378589d 100644
--- a/sandbox_test.cpp
+++ b/sandbox_test.cpp
@@ -23,8 +23,22 @@
 
 #include "test_tools.hpp"
 
+#include "array"
+
+template<typename T>
+T foo(int i, T& t = std::array<T,1>()[0])
+{return t = T(i);}
+
 int test_main(int, char*[])
 {
+    double x = 3;
+    foo(10, x);
+    BOOST_TEST_EQUAL(10, x);
+    BOOST_TEST_EQUAL(11, foo(11, x));
+    BOOST_TEST_EQUAL(12, foo<double>(12, x));
+    BOOST_TEST_EQUAL(13, foo<double>(13));
+    double const y = foo<double>(14);
+    BOOST_TEST_EQUAL(14, y);
     return 0;
 }
 
--------->8-------->8-------->8-------->8-------->8-------->8-------->8-------

although adding another overload truly is wiser.

>  It looks like you're really looking for what had been suggested before,
> i.e.
> 
>       template<typename T>
>       T product_database::query_into(e_database_key k) const;
> 
> which could then be used as
> 
>       auto const alb_anb = query_into<oenum_alb_or_anb>(DB_AgeLastOrNearest);
> 
>  Or am I missing something?

No, that's perfect: a single statement, and it's const.

Nevertheless, I do want to swap the order of the arguments. If there's
a single common argument for all downloads, it just seems more fitting
that it be written first.



reply via email to

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