On 18/09/18 13:08, bol...@cylonHQ.com wrote:
> On Tue, 18 Sep 2018 12:26:07 +0200
> David Brown <
david...@hesbynett.no> wrote:
>> On 18/09/18 11:33, bol...@cylonHQ.com wrote:
>>>> that have no obvious connection to the duck-like features you want.
>>>> Enter "concepts" that give you this, and you have many of the benefits
>>>> of duck-typing in a statically compiled and checked language.
>>>
>>> If you want duck typing just use python.
>>
>> If you want fast code with static checking, /and/ you want some aspects
>> of duck-typing, use C++ with concepts.
>
> Or use Objective-C which has had it since it was created. Oddly though that
> language never took off outside the apple ecosystem.
>
>> language". It is a /good/ thing that languages take inspiration from
>> each other to see how they can be better for developers.
>
> You say taking inspiration, I say a one-size-fits-all type language which
> will never work.
Nobody has suggested a one-size-fits-all language. People have merely
suggesting adding some more features to a useful and flexible language.
>
>>> Whoppee, I can hardly wait. Serialised json is just json. Its not binary, it
>>
>>> doesn't have any word byte order conversions to worry about nor does it store
>>
>>> BLOBs. If you simply want to compress it then you could do that in C, never
>>> mind C++.
>>
>> We all know what JSON is. Would it not be lovely to be able to take an
>> object in C++ and pass it to a generic "toJson" function and have its
>> members serialised - recursively as needed - without having to write
>> specialised functions for every single class in the program?
>
> It would, except it'll never be implemented fully. Why? Pointers. Apart from
> not being able to figure out the pointer type unless extra functionality is
> added to the runtime system (typeid is no good as you have to know beforehand
> what you're checking against), the pointer itself could be pointing at anything
> or anywhere. Eg shared memory, of which the structure is only known to the
> program logic, not the compiler or any library function and could in turn have
> pointers itself. Serialising that would simply produce a meaningless list of
> values that could be invalid in any other process.
>
> So, even if built in serialisation is added it'll be rather restricted.
Of course it would be restricted (and pointers are not the main issue -
especially since they could probably be avoided in most cases). But it
would still be very, very useful.
>
>>> Had to look up enable_if, never heard of it. If your templates get that
>>> complex then maybe for sanitys sake just bin them and use non templated
>>> functions and classes even if it means more code.
>>>
>>
>> Have you ever looked at how the standard library is implemented? You
>
> Once, it was enough.
>
>> may not need to use safe boolean idioms, enable_if, and the rest in your
>> code - but I'm guessing that you have at least used standard containers
>> that /do/ use this kind of thing for their implementation. If you don't
>
> Since enable_if is C++ 2011 they quite obviously don't (or didn't) as the STL
> has been around since the 90s. Seems to me it is nothing more than yet another
> syntatic hack to get around the poor initial syntactic and lexical design of
> some aspect of the language rather than anything that will aid in developing
> actually useful code or algorithms.
>
You might want to look at what has changed over the years, before
deciding that what you had in the 1990's was enough. And "enable_if"
does not actually do anything that could not be done before - it's usual
definition is simple enough and valid for C++98 (I think). But it
standardises a common pattern with a recognizable name, making such
template code easier to read, write and maintain..
Many new C++ features are not about allowing you to do something that
you could not do earlier - they are about making them simple, clearer,
and easier to work with.
Let's take a little example. Suppose you want a function "roughlyEqual"
that compares integers or floats, using a different algorithm in each
case. With concepts, you can define a couple of concepts and write out
the functions neatly. (There are severe limitations in this example -
it is a taster, nothing more.)
You can paste this into <
https://godbolt.org> and compile with "-fconcepts".
#include <type_traits>
#include <cstdlib>
// These "concepts" would probably be part of the standard library
template <typename T>
concept bool Floating_Point =
std::is_floating_point<T>::value;
template <typename T>
concept bool Integer =
std::is_integral<T>::value;
// Here we have two template functions using the concepts
bool roughlyEqual(Floating_Point a, Floating_Point b) {
return abs(a - b) < 0.0001;
}
bool roughlyEqual(Integer a, Integer b) {
return a == b;
}
// And some test code
bool test1(void) {
return roughlyEqual(2, 5);
}
bool test2(void) {
return roughlyEqual(2, 2);
}
bool test3(void) {
return roughlyEqual(2.0, 5.0);
}
bool test4(void) {
return roughlyEqual(2.0, 2.000001);
}
If you try "roughlyEqual("a", "b")", it will fail to compile - which is
the result you want.
You can write the roughlyEqual template functions using SFINAE, using
enable_if to make it easier:
template <typename T>
typename std::enable_if<std::is_integral<T>::value, bool>::type
roughlyEqual(T a, T b) {
return a == b;
}
template <typename T>
typename std::enable_if<std::is_floating_point<T>::value, bool>::type
roughlyEqual(T a, T b) {
return abs(a - b) < 0.0001;
}
To me, the concept code is a lot simpler.