Are there political concepts?

While working through Section 1 Modern C ++ Design (Alexandrescu, 2001), the following thought came to my mind: Aren't concepts just concepts?

If I understand correctly, a concept is a set of interface requirements for the classes that implement the concept.

A policy is also a specification that policy classes must satisfy. (although some policy classes may provide an enriched interface, I'm not sure if this makes policy out of the scope of the concept)

+3


source to share


2 answers


You need to consider what are the goals of both the concepts and the policies.

Concepts establish strong assumptions about a class, both syntactically and semantically. For example, Comparable

it implies the presence of comparison operators for the type, as well as the fact that some axioms are preserved, i.e. a == b -> a is equivalent to b

rather than any other implementation operator==

. Strong assumptions make your code more predictable.

Policymakers, on the other hand, establish the actual behavior of the class with an emphasis on fungibility. Each individual policy implementation encompasses one or more concepts; however, the policy variations you connect to the class only share the constraints. For example (pseudocode):

// constraint Lockable -> has Lock() and Unlock() methods

// concept RealLockable -> constrained by Lockable, Lock() blocks other threads
// concept FakeLockable -> constrained by Lockable, Lock() does nothing

class FakeLock; // a policy that embodies the concept FakeLockable
class Lock; // a policy that embodies the concept RealLockable
class RecursiveLock; // a policy that can also embody the concept of RealLockable

template<class T, Lockable Lock> // constrained by Lockable
class Queue;

      



Policy is an implementation method that allows you to change the behavior of the target class. The concept is an explicit tool for determining assumptions:

template<Comparable T>
class PriorityQueue;

      

Comparable T

would not be a policy in my opinion, because when you specify PriorityQueue<int>

or PriorityQueue<double>

, you are not aiming to change the behavior: conceptually, all of these queues allow you to access the minimum or maximum element at a constant time. However, with some future syntax, you might assert that it T

embodies mathematically correct semantics, i.e. a < b -> b > a

and then there Comparable T

will be a concept.

After all, every class, politics or not, is a concept or two. Classes introduce concepts implicitly; concepts document them explicitly.

+3


source


As I understand from this book, policy-driven development is not entirely accurate in terms of differentiation from concepts. Simply because the latter term came later historically.

Alexandrescu's policy is an early precursor to what is understood today.

Although there is a line:



The concepts will never change the structure of the instances, while the policies presented in the book can.

To some extent, I think the concept describes the interface, while the policy can be involved in the implementation of the interfaces.

0


source







All Articles