I have been reading up on Concept based Inheritance in C++. I have a attached a code sample for all. I am basically asking if this is a correct implementation of the concept of this? I am new to this so I am just putting down what is in my mind. Any comments / critisims are welcome.
#include "stdafx.h"
#include <memory>
#include <vector>
#include <algorithm>
#include <iostream>
using namespace std;
struct Point{
int x;
int y;
};
class graphics_surface{
class drawable_concept{
public:
virtual void draw(Point const& coordinate) {};
virtual ~drawable_concept() {};
};
template<class T>
class drawable_model : public drawable_concept{
public:
drawable_model(T& item) : item_(item){}
void draw(Point const& coordinate){
item_.draw(coordinate);
}
~drawable_model(){}
private:
T item_;
};
public:
template<class T>
void push_back(T& drawable){
v_.push_back(shared_ptr<drawable_concept>( new drawable_model<T>(drawable)));
}
void draw(Point const& coordinate) {
for_each(v_.begin(), v_.end(), [&](shared_ptr<drawable_concept>& concept){
concept->draw(coordinate);
});
}
private:
vector<shared_ptr<drawable_concept>> v_;
};
struct triangle{
void draw(Point const& p){
cout << "Triangle: " << p.x << "," << p.y << endl;
}
};
struct square{
void draw(Point const& p){
cout << "Sqaure: " << p.x << "," << p.y << endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
Point p;
p.x = 1;
p.y = 2;
graphics_surface surface;
surface.push_back(triangle());
surface.draw(p);
return 0;
}
Thanks in advance.
Blair
-
3I don't see anything about concepts in there. It's just normal class-based polymorphism (whatever the proper name for that is). (edit: oh, now I see how this works... this comment might be completely wrong)Stack Exchange Broke The Law– Stack Exchange Broke The Law2014年02月17日 06:19:09 +00:00Commented Feb 17, 2014 at 6:19
-
Note: "Concepts" is strongly associated by C++ programmers with the C++0x proposal for checking that types passed as template parameters supported specific semantics; this question is about "Concept Based Polymorphism" which is a distinct idiom for using compile-time polymorphism to create a run-time polymorphic object.Tony Delroy– Tony Delroy2014年02月17日 07:04:08 +00:00Commented Feb 17, 2014 at 7:04
-
Yep this is what I am talking about Tony D. Keen for comments.Blair Davidson– Blair Davidson2014年02月17日 07:18:41 +00:00Commented Feb 17, 2014 at 7:18
-
The tag "c++-concepts" is supposed to be about the c++0x/y proposed language feature.n. m. could be an AI– n. m. could be an AI2014年02月17日 07:47:11 +00:00Commented Feb 17, 2014 at 7:47
-
1Updated the example with a gist at gist.github.com/loosechainsaw/9049615Blair Davidson– Blair Davidson2014年02月17日 12:31:42 +00:00Commented Feb 17, 2014 at 12:31
2 Answers 2
A few points:
I don't see any good reason to put
drawable_concept
ordrawable_model
insidegraphics_surface
- you just prevent reuse of something that's potentially useful in other container types...you have some
const
issuesdraw
should probably beconst
(and function definitions should not be followed by semicolons ;-)drawable_model(T& item)
should takeitem
byconst
referencepush_back(T& drawable)
shoudl takedrawable
byconst
reference
you should use
make_shared
for exception safetythe "factory" functionality would arguably be better off separated into a separate function, rather than buried inside
push_back
5 Comments
Your approach here is more about type erasure than it is about Concept based programming. It's an extension of the idea used by boost::any. Concepts are a set of constraints on a type required by a class or function template. The STL has concepts such as ForwardIterator and InputIterator. These are constraints that are expected to be true for parameters passed to some std algorithms for example.