Home » U++ Library support » U++ Core » Question about SubRange.
Question about SubRange. [message #49191] |
Thu, 04 January 2018 21:58 |
Novo
Messages: 1358 Registered: December 2006
|
Ultimate Contributor |
|
|
Function SubRange has two overloads below.
template <class C>
auto SubRange(C& c, int pos, int count) -> decltype(SubRange(c.begin() + pos, count))
{
return SubRange(c.begin() + pos, count);
}
template <class C>
auto SubRange(C&& c, int pos, int count) -> decltype(SubRange(c.begin() + pos, count))
{
return SubRange(c.begin() + pos, count);
}
The second one doesn't actually move anything, so, it is just not needed, IMHO.
The first one can always be used instead. And it should look like below. IMHO.
template <class C>
auto SubRange(const C& c, int pos, int count) -> decltype(SubRange(c.begin() + pos, count))
{
return SubRange(c.begin() + pos, count);
}
Method begin() is always const I believe ...
And you can drop this decltype in C++14 ...
Regards,
Novo
|
|
|
Re: Question about SubRange. [message #49218 is a reply to message #49191] |
Mon, 08 January 2018 11:52 |
|
mirek
Messages: 13975 Registered: November 2005
|
Ultimate Member |
|
|
Novo wrote on Thu, 04 January 2018 21:58Function SubRange has two overloads below.
template <class C>
auto SubRange(C& c, int pos, int count) -> decltype(SubRange(c.begin() + pos, count))
{
return SubRange(c.begin() + pos, count);
}
template <class C>
auto SubRange(C&& c, int pos, int count) -> decltype(SubRange(c.begin() + pos, count))
{
return SubRange(c.begin() + pos, count);
}
The second one doesn't actually move anything, so, it is just not needed, IMHO.
The first one can always be used instead. And it should look like below. IMHO.
template <class C>
auto SubRange(const C& c, int pos, int count) -> decltype(SubRange(c.begin() + pos, count))
{
return SubRange(c.begin() + pos, count);
}
Method begin() is always const I believe ...
Not true (not in U++ nor STL). That is basically a reason for those 2 overloads. E.g. try Sort with just const variant...
Quote:
And you can drop this decltype in C++14 ...
Not if we want to maintain C++11 compatibility (current status is that we do and we will as long as it is cheap. I think decltype here is cheap).
|
|
|
Re: Question about SubRange. [message #49221 is a reply to message #49218] |
Mon, 08 January 2018 15:55 |
Novo
Messages: 1358 Registered: December 2006
|
Ultimate Contributor |
|
|
mirek wrote on Mon, 08 January 2018 05:52
Not true (not in U++ nor STL). That is basically a reason for those 2 overloads. E.g. try Sort with just const variant...
The "C& c" variant is definitely not needed. The "C&& c" variant is an universal reference and it is a complete replacement of the "C& c" variant. I tried to compile TheIDE with "C& c" commented out and it compiles just fine.
The problem with the "const C& c" variant is that when you try to construct the SubRangeClass you get a SubRangeClass<const I> version.
I tried to strip the constness like below
template <class I>
SubRangeClass<I> SubRange(I l, I h)
{
return SubRangeClass<typename std::remove_const<I>::type>(l, h);
}
template <class I>
SubRangeClass<I> SubRange(I l, int count)
{
return SubRangeClass<typename std::remove_const<I>::type>(l, count);
}
But I'm still getting weird compilation problems with TabBar::Tab.
And I'm still a little bit confused about what is IterSwap for. What is wrong with the regular Swap?
Something is not right about all this ...
Regards,
Novo
|
|
|
|
|
Goto Forum:
Current Time: Thu Mar 28 16:03:00 CET 2024
Total time taken to generate the page: 0.01096 seconds
|