need help with unfamiliar syntax syntax

so I've been reading SFML blueprints and I've come across
a syntax unfamiliar to me,
I understand that the struct is inheriting something but there's that arrowy
enclosed syntax I dont know about, can anyone shed some light one this?
what is <CompAIWarrior>?
and what is it for/do?

1
2
3
4
5
6
7
8
9
10
struct CompAIWarrior : Component<CompAIWarrior> <- what is this?
{
explicit CompAIWarrior(int hitPoint,const sf::Time&
timeDelta,int range);
const int _hitPoint;
const sf::Time _delta;
sf::Time _elapsed;
const int _range;
};
what is <CompAIWarrior>?
Actually Component is a template class/struct and CompAIWarrior is the template parameter.
So Component knows how to deal with the CompAIWarrior type. Maybe CompAIWarrior has certain members that Component uses.
This is an interesting pattern. Does anybody know what it's used for?

1
2
3
4
5
6
7
8
template <typename T>
struct B {
    // ...
};

struct D : B<D> {   // use derived class itself as template parameter of base
    // ...
};

Search for the "Curiously recurring template pattern" or "F-bounded quantification".

See:
http://www.cplusplus.com/forum/general/220370/
http://www.cplusplus.com/forum/general/220967/
Could be used for comparisons with other T's or similarly demonstrating that derived children will have a particular behavior. I imagine the bases of such designs are very short and contain at least one pure method (to enforce said behavior).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <string>

using namespace std;

template<typename T>
struct Comparable
{
    virtual bool operator< (const T& other) = 0;
};

class Integer : Comparable<Integer> 
{
public:
    Integer(int n, string name) : 
        n_(n), 
        name_(name)
    {        
    }
    
    bool operator< (const Integer& other) override
    {
        return n_ < other.n_;
    }
private:
    int n_;
    string name_;
};

int main()
{
    Integer i(24, "twenty-four");
    Integer j(36, "thirty-six");

    cout << boolalpha;
    cout << "i<j ? " << (i<j) << endl;
    
    return 0;
}
Last edited on
Curiouser and curiouser! Thanks guys.
Topic archived. No new replies allowed.