Home » U++ Library support » U++ Core » How to mark std::array<T, N> moveable if only T is moveable
Re: How to mark std::array<T, N> moveable if only T is moveable [message #60696 is a reply to message #60682] |
Tue, 23 July 2024 18:25   |
busiek
Messages: 70 Registered: February 2011 Location: Poland
|
Member |
|
|
mirek wrote on Thu, 18 July 2024 08:41Lance wrote on Wed, 03 July 2024 03:39Reminds me of some exploration I did with Moveable.
I vaguely recalled that when I tested a few years back, the way Moveable was designed were causing a compile time error with Windows/VSBuild. I would also welcome a redesign of Moveable using more recent language facilities.
Suggestions? (But it needs to be backward compatible).
I was thinking about something like that:
#include <Core/Core.h>
#include <type_traits>
namespace Upp {
// First way of marking class moveable is to be an ancestor of NtlMoveableBase
template <class T> class NtlMoveableBase {};
// Second way is to specialize NtlMoveableClass
template <class T>
struct NtlMoveableClass
: std::integral_constant<bool,
std::is_trivial_v<T>
|| std::is_base_of_v<Upp::NtlMoveableBase<T>, T>
// below is not needed if we make them derived from NtlMoveableBase
|| std::is_base_of_v<Upp::Moveable_<T>, T>
|| std::is_base_of_v<Upp::Moveable<T>, T>
|| std::is_base_of_v<Upp::MoveableAndDeepCopyOption<T>, T>> {};
// For instance mark std::array<T, N> moveable if only T is moveable
template <class T, size_t N>
struct NtlMoveableClass<std::array<T, N>>
: std::integral_constant<bool, NtlMoveableClass<T>::value> {};
// Helper for checking whether class is moveable
template <class T>
inline constexpr bool IsNtlMoveable = NtlMoveableClass<T>::value;
// Optional concept for c++20
template <class T>
concept NtlMoveable = IsNtlMoveable<T>;
}
using namespace Upp;
// use of concept
template <NtlMoveable T>
struct MyVector
{
T *ptr;
// Or use static_assert
~MyVector() { static_assert(IsNtlMoveable<T>); }
};
CONSOLE_APP_MAIN
{
// Checking if given type is moveable
static_assert(IsNtlMoveable<int>);
static_assert(IsNtlMoveable<const void *>);
static_assert(IsNtlMoveable<Vector<int>>);
static_assert(IsNtlMoveable<std::array<int, 5>>);
static_assert(IsNtlMoveable<std::array<Vector<int>, 5>>);
static_assert(!IsNtlMoveable<Thread>);
}
You could redefine NTL_MOVEABLE macro as a specialization of NtlMoveableClass.
But you need to change an assertion AssertMoveable((T*)0) to static_assert(IsNtlMoveable<T>).
Are those ideas usable?
|
|
|
 |
|
How to mark std::array<T, N> moveable if only T is moveable
By: busiek on Tue, 02 July 2024 17:54
|
 |
|
Re: How to mark std::array<T, N> moveable if only T is moveable
By: Lance on Wed, 03 July 2024 03:39
|
 |
|
Re: How to mark std::array<T, N> moveable if only T is moveable
By: mirek on Thu, 18 July 2024 08:41
|
 |
|
Re: How to mark std::array<T, N> moveable if only T is moveable
By: busiek on Tue, 23 July 2024 18:25
|
 |
|
Re: How to mark std::array<T, N> moveable if only T is moveable
By: mirek on Thu, 25 July 2024 02:27
|
 |
 |
Re: How to mark std::array<T, N> moveable if only T is moveable
By: Lance on Thu, 25 July 2024 03:16
|
 |
|
Re: How to mark std::array<T, N> moveable if only T is moveable
By: mirek on Thu, 25 July 2024 09:19
|
 |
|
Re: How to mark std::array<T, N> moveable if only T is moveable
By: mirek on Thu, 25 July 2024 09:32
|
 |
|
Re: How to mark std::array<T, N> moveable if only T is moveable
By: mirek on Thu, 25 July 2024 10:07
|
 |
|
Re: How to mark std::array<T, N> moveable if only T is moveable
By: Lance on Fri, 26 July 2024 00:00
|
 |
|
Re: How to mark std::array<T, N> moveable if only T is moveable
By: busiek on Thu, 25 July 2024 17:32
|
 |
|
Re: How to mark std::array<T, N> moveable if only T is moveable
By: mirek on Mon, 29 July 2024 08:47
|
 |
|
Re: How to mark std::array<T, N> moveable if only T is moveable
By: mirek on Fri, 23 August 2024 08:54
|
 |
|
Re: How to mark std::array<T, N> moveable if only T is moveable
By: mirek on Thu, 18 July 2024 08:44
|
 |
|
Re: How to mark std::array<T, N> moveable if only T is moveable
By: busiek on Thu, 18 July 2024 19:39
|
 |
|
Re: How to mark std::array<T, N> moveable if only T is moveable
By: mirek on Fri, 19 July 2024 13:11
|
 |
|
Re: How to mark std::array<T, N> moveable if only T is moveable
By: Lance on Fri, 19 July 2024 13:52
|
 |
|
Re: How to mark std::array<T, N> moveable if only T is moveable
By: Lance on Fri, 19 July 2024 14:02
|
 |
|
Re: How to mark std::array<T, N> moveable if only T is moveable
By: Didier on Fri, 19 July 2024 19:10
|
 |
|
Re: How to mark std::array<T, N> moveable if only T is moveable
By: mirek on Mon, 22 July 2024 11:10
|
 |
|
Re: How to mark std::array<T, N> moveable if only T is moveable
By: Lance on Tue, 23 July 2024 01:07
|
Goto Forum:
Current Time: Thu Aug 07 22:08:31 CEST 2025
Total time taken to generate the page: 0.04254 seconds
|