Wednesday, September 4, 2019

Digest for comp.lang.c++@googlegroups.com - 8 updates in 4 topics

Ian Collins <ian-news@hotmail.com>: Sep 05 06:59AM +1200

On 05/09/2019 00:58, Daniel wrote:
> 3
 
> I don't understand why one of these constructors is using the
> initializer_list, and the other is not. Explanation?
 
Does the compiler issue any warnings?
 
gcc gives your expected result (2,4), clang thinks you intended to call
A(arg_t, size_t n) and issues a warning
 
/tmp/x.cc:40:15: warning: braces around scalar initializer
[-Wbraced-scalar-init]
A b{ arg, { 2 } }; // (2)
^~~~~~
 
I'm not sure which one is correct in this case!
 
Cheers,
Ian.
Daniel <danielaparker@gmail.com>: Sep 04 12:48PM -0700

On Wednesday, September 4, 2019 at 2:59:50 PM UTC-4, Ian Collins wrote:
> A b{ arg, { 2 } }; // (2)
> ^~~~~~
 
> I'm not sure which one is correct in this case!
 
Thanks for the observations, I thought I understood the issues with the
uniform initialization and std::initializer_list, but I don't understand this
result. vs2019 doesn't give any warnings at level 4, apart from my
unreferenced parameters.
 
Best regards,
Daniel
Juha Nieminen <nospam@thanks.invalid>: Sep 04 01:55PM

In C++17 you can do this:
 
//-----------------------------------------------------
#include <iostream>
#include <algorithm>
#include <functional>
 
int main(int argc, char** argv)
{
double values[5] = { 1.1, 2.2, 3.3, 4.4, 5.5 };
 
// Notice this:
std::sort(values, values+5, std::greater());
 
for(double v: values) std::cout << v << " ";
std::cout << "\n";
}
//-----------------------------------------------------
 
You don't actually need to specify the template parameter of std::greater.
It will be automatically deduced as 'double'. That's really nice.
 
But wait... How?!?
 
There's nothing telling std::greater that the template parameter should be
of type double. It's not taking any constructor parameters or anything.
And the declaration of std::sort() is ostensibly like this:
 
template<class RandomIt, class Compare>
void sort(RandomIt first, RandomIt last, Compare comp);
 
so there's nothing there telling it that it should be double either.
 
So how?
Ian Collins <ian-news@hotmail.com>: Sep 05 07:16AM +1200

On 05/09/2019 01:55, Juha Nieminen wrote:
> void sort(RandomIt first, RandomIt last, Compare comp);
 
> so there's nothing there telling it that it should be double either.
 
> So how?
 
Magic? I assume it's related to the changes in C++17 which extend
template argument deduction to template classes as well as functions.
 
All of the information is there, given the signature of Compare is bool
comp(const Type1 &a, const Type2 &b) and Type1 and Type2 can be deduced
from RandomIt.
 
--
Ian.
Bonita Montero <Bonita.Montero@gmail.com>: Sep 04 04:02PM +0200

> not be modifiable (eg. because it's in a third-party library), and in
> some situations the members might not be assignable, most typically
> because you want/need the instantiation to be const (or even constexpr):
 
One solution would be to derive a class from the structure and have
a constructor on it. The APIs evaluating the structure will get the
upcasted structure.
 
> ...
> const Data kData = createData(5, "hello");
> but that's a long-winded way of doing it.
 
Ok, you're right. That's days of work!
Bonita Montero <Bonita.Montero@gmail.com>: Sep 04 05:22PM +0200


> One solution would be to derive a class from the structure and have
> a constructor on it. The APIs evaluating the structure will get the
> upcasted structure.
 
BTW: Maybe this could fail if the API requires an array of structures.
Or is it guaranteed that a derived class without any data-members always
has the same size as the base-class? Practically this will apply to all
compilers, but theoretically that might not be correct.
Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo>: Sep 04 10:47AM -0400

Öö Tiib wrote:
>> configuration would help)
 
> What was the question or issue? Did read 3 times over but somehow missed
> it. :( Perhaps I need a coffee.
 
I want Caller<IA, 1>::callMethod to have prototype
void Caller<IA, 1>::callMethod<void (IA::*)(I, I), I, I>(void (IA::*)(I,
I), I, I)
 
,
 
Caller<IA, 2>::callMethod to have prototype
void Caller<IA, 2>::callMethod<void (IA::*)(I const&, I const&), I const&,
I const&>(void (IA::*)(I const&, I const&), I const&, I const&)
 
and
 
Caller<IA, 3>::callMethod to have prototype
void Caller<IA, 3>::callMethod<void (IA::*)(I const&, I), I const&,
I>(void (IA::*)(I const&, I), I const&, I)
 
In other words, I want callMethod generated from the callMethod method
template to take exactly same parameter types for its last 2 parameters
as does the interface method to which the callMethod method forwards.
 
I could not achieve this by any of callMethod template definitions I
tried. E.g. the currently-uncommented in my example definition generates
the methods with the prototypes shown in the original post (1st and 3rd
of which are different from the above).
Pavel <pauldontspamtolk@removeyourself.dontspam.yahoo>: Sep 04 11:08AM -0400

Vir Campestris wrote:
> _not_ upgrading has become too much... you realise GCC 4.8 came out in
> 2013, and all you've had since then is bug fixes? And not all bugs get
> fixed?
Yes. In this case I am the wrong tree to ... blame :-) for non-upgrading.
 
Regardless, I have not yet got a solution for any compiler. If somebody
threw at me a solution that worked on a later compiler but not gcc
4.8.5, it would be a good addition to my list of reasons for the upgrade.
 
You received this digest because you're subscribed to updates for this group. You can change your settings on the group membership page.
To unsubscribe from this group and stop receiving emails from it send an email to comp.lang.c+++unsubscribe@googlegroups.com.

No comments: