Another update from ingydotnet^djgoku
This commit is contained in:
parent
91df62d461
commit
948b86eafa
7604 changed files with 108452 additions and 22726 deletions
33
Task/Constrained-genericity/C++/constrained-genericity.cpp
Normal file
33
Task/Constrained-genericity/C++/constrained-genericity.cpp
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
template<typename T> //Detection helper struct
|
||||
struct can_eat //Detects presence of non-const member function void eat()
|
||||
{
|
||||
private:
|
||||
template<typename U, void (U::*)()> struct SFINAE {};
|
||||
template<typename U> static char Test(SFINAE<U, &U::eat>*);
|
||||
template<typename U> static int Test(...);
|
||||
public:
|
||||
static constexpr bool value = sizeof(Test<T>(0)) == sizeof(char);
|
||||
};
|
||||
|
||||
struct potato
|
||||
{ void eat(); };
|
||||
|
||||
struct brick
|
||||
{};
|
||||
|
||||
template<typename T>
|
||||
class FoodBox
|
||||
{
|
||||
//Using static assertion to prohibit non-edible types
|
||||
static_assert(can_eat<T>::value, "Only edible items are allowed in foodbox");
|
||||
|
||||
//Rest of class definition
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
FoodBox<potato> lunch;
|
||||
|
||||
//Following leads to compile-time error
|
||||
//FoodBox<brick> practical_joke;
|
||||
}
|
||||
61
Task/Constrained-genericity/Forth/constrained-genericity.fth
Normal file
61
Task/Constrained-genericity/Forth/constrained-genericity.fth
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
include FMS-SI.f
|
||||
include FMS-SILib.f
|
||||
|
||||
: (where) ( class-xt where-dfa -- flag )
|
||||
swap >body { where-dfa class-dfa }
|
||||
begin
|
||||
class-dfa ['] object >body <>
|
||||
while
|
||||
class-dfa where-dfa = if true exit then
|
||||
class-dfa sfa @ to class-dfa
|
||||
repeat false ;
|
||||
|
||||
: where ( class-xt "classname" -- flag )
|
||||
' >body state @
|
||||
if postpone literal postpone (where)
|
||||
else (where)
|
||||
then ; immediate
|
||||
|
||||
:class Eatable
|
||||
:m eat cr ." successful eat" ;m
|
||||
;class
|
||||
|
||||
\ FoodBox is defined without using eat in any way.
|
||||
:class FoodBox
|
||||
object-list eatable-types
|
||||
:m fill: { n class-xt -- }
|
||||
class-xt where Eatable
|
||||
if n 0 do class-xt eatable-types xtadd: loop
|
||||
else ." not an eatable type "
|
||||
then ;m
|
||||
:m get ( -- obj ) eatable-types ;m
|
||||
;class
|
||||
|
||||
: test ( obj -- ) \ send the eat message to each object in the object-list
|
||||
begin dup each:
|
||||
while eat
|
||||
repeat drop ;
|
||||
|
||||
FoodBox fb
|
||||
3 ' Eatable fb fill: \ fill the object-list with 3 objects of class Eatable
|
||||
fb get test
|
||||
successful eat
|
||||
successful eat
|
||||
successful eat
|
||||
|
||||
FoodBox fb1
|
||||
5 ' object fb1 fill: \ => not an eatable type
|
||||
|
||||
:class apple <super Eatable
|
||||
;class
|
||||
|
||||
:class green-apple <super apple
|
||||
;class
|
||||
|
||||
5 ' green-apple fb1 fill:
|
||||
fb1 get test
|
||||
successful eat
|
||||
successful eat
|
||||
successful eat
|
||||
successful eat
|
||||
successful eat
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
@protocol Eatable
|
||||
- (void)eat;
|
||||
@end
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
@interface FoodBox<T : id<Eatable>> : NSObject
|
||||
@end
|
||||
Loading…
Add table
Add a link
Reference in a new issue