Home » Extra libraries, Code snippets, applications etc. » C++ language problems and code snippets » Make THISFN simpler and more powerful (by taking advantage of some new c++ feature)
Re: Make THISFN simpler and more powerful [message #59153 is a reply to message #59037] |
Thu, 10 November 2022 00:32   |
Lance
Messages: 656 Registered: March 2007
|
Contributor |
|
|
This is kind of C++20 related, I am going to reuse this thread for c++ language specific problems or what not.
Here I got a problem: how to detect if a class is Moveable?
A naive first thought would be
#include <Core/Core.h>
#include <concepts>
template <class T>
inline constexpr bool is_moveable = std::derived_from<T, Upp::Moveable<T>>;
using namespace Upp;
CONSOLE_APP_MAIN
{
LOG(is_moveable<Upp::Rect>);
LOG(is_moveable<Upp::String>);
}
Output
true
false
It turns out Moveable has an optional second template parameter which failed the test in [b]String[/]'s case.
What makes Moveable so special is that it introduced a friend AssertMoveable0 in <Core/Topt.h>
template <class T>
inline void AssertMoveable0(T *t) { AssertMoveablePtr(&**t, *t); }
// COMPILATION ERROR HERE MEANS TYPE T WAS NOT MARKED AS Moveable
template <class T, class B = EmptyClass>
struct Moveable : public B {
friend void AssertMoveable0(T *) {}
};
Trying to use it directly, I come up with something like
#include <Core/Core.h>
#include <concepts>
template <class T>
inline constexpr bool is_moveable = /*std::derived_from<T, Upp::Moveable<T>>;*/
requires (T t){ Upp::AssertMoveable0(&t); };
using namespace Upp;
CONSOLE_APP_MAIN
{
LOG(is_moveable<Upp::Rect>);
LOG(is_moveable<Upp::String>);
}
This time both give correct result, but unfortuately, any class T would give a true result regardless of whether it's derived from Upp::Moveable.
How would you check if a class type is Upp::Moveable ?
BTW, something like this
LOG( std::is_base_of<Ctrl, EditField>);
will fail to compile because of a missing guard in the definition of LOG in <Core/Diag.h>
#define LOG(a) UPP::VppLog() << a << UPP::EOL
I would think here change it to
#define LOG(a) UPP::VppLog() << (a) << UPP::EOL
might be a reasonable thing to do.
|
|
|
 |
 |
Make THISFN simpler and more powerful
By: Lance on Tue, 18 October 2022 21:08
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Thu, 10 November 2022 00:32
|
 |
|
Re: Make THISFN simpler and more powerful
By: Oblivion on Thu, 10 November 2022 06:29
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Thu, 10 November 2022 12:52
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Thu, 10 November 2022 21:41
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Fri, 11 November 2022 02:50
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Tue, 08 October 2024 18:38
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Tue, 08 October 2024 18:42
|
 |
|
Re: Make THISFN simpler and more powerful
By: Didier on Tue, 08 October 2024 19:25
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Tue, 08 October 2024 20:01
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Wed, 09 October 2024 05:59
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Wed, 09 October 2024 16:09
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Wed, 09 October 2024 16:17
|
 |
|
Re: Make THISFN simpler and more powerful
By: Didier on Wed, 09 October 2024 19:32
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Thu, 10 October 2024 14:01
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Thu, 10 October 2024 14:06
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Tue, 05 November 2024 02:25
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Tue, 05 November 2024 02:33
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Thu, 07 November 2024 00:49
|
 |
|
Re: Make THISFN simpler and more powerful
By: Lance on Fri, 29 November 2024 02:30
|
Goto Forum:
Current Time: Mon Jul 07 18:28:03 CEST 2025
Total time taken to generate the page: 0.04375 seconds
|