Visual Computing Library
devel
Version:
Loading versions...
Loading...
Searching...
No Matches
polymorphism.h
1
/*****************************************************************************
2
* VCLib *
3
* Visual Computing Library *
4
* *
5
* Copyright(C) 2021-2025 *
6
* Visual Computing Lab *
7
* ISTI - Italian National Research Council *
8
* *
9
* All rights reserved. *
10
* *
11
* This program is free software; you can redistribute it and/or modify *
12
* it under the terms of the Mozilla Public License Version 2.0 as published *
13
* by the Mozilla Foundation; either version 2 of the License, or *
14
* (at your option) any later version. *
15
* *
16
* This program is distributed in the hope that it will be useful, *
17
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
18
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19
* Mozilla Public License Version 2.0 *
20
* (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21
****************************************************************************/
22
23
#ifndef VCL_BASE_CONCEPTS_POLYMORPHISM_H
24
#define VCL_BASE_CONCEPTS_POLYMORPHISM_H
25
26
#include <concepts>
27
#include <memory>
28
29
namespace
vcl {
30
50
template
<
typename
T>
51
concept
Cloneable
=
requires
(T&& obj) {
52
// TODO: Right now, this concept can be used only with a base class that has
53
// a clone method, because the concept requires that the return type of the
54
// clone method is a shared pointer to the same class as the object. This is
55
// not always the case, especially when the clone method is overridden in
56
// derived classes. We need to find a way to make this concept work with
57
// overridden clone methods.
58
//
59
// However, we should also consider the consequences of this change for the
60
// class vcl::PolymorphicObjectVector, which relies on this concept to
61
// determine if an object can be cloned, and stores objects of the Base
62
// class in a vector.
63
//
64
// Example:
65
// class Base {
66
// public:
67
// virtual std::shared_ptr<Base> clone() const = 0;
68
// };
69
//
70
// class Derived : public Base {
71
// public:
72
// std::shared_ptr<Derived> clone() const { ... }
73
// };
74
//
75
// static_assert(vcl::Cloneable<Base>, ""); // OK
76
// static_assert(vcl::Cloneable<Derived>, ""); // Error, but should work
77
{ obj.clone() } -> std::same_as<std::shared_ptr<std::remove_cvref_t<T>>>;
78
};
79
80
}
// namespace vcl
81
82
#endif
// VCL_BASE_CONCEPTS_POLYMORPHISM_H
vcl::Cloneable
Concept that is evaluated true if T is a cloneable object.
Definition
polymorphism.h:51
vclib
core
include
vclib
base
concepts
polymorphism.h
Generated by
1.9.8