9

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

Tony Delroy
107k16 gold badges188 silver badges265 bronze badges
asked Feb 17, 2014 at 6:08
7
  • 3
    I 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) Commented 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. Commented Feb 17, 2014 at 7:04
  • Yep this is what I am talking about Tony D. Keen for comments. Commented Feb 17, 2014 at 7:18
  • The tag "c++-concepts" is supposed to be about the c++0x/y proposed language feature. Commented Feb 17, 2014 at 7:47
  • 1
    Updated the example with a gist at gist.github.com/loosechainsaw/9049615 Commented Feb 17, 2014 at 12:31

2 Answers 2

2

A few points:

  • I don't see any good reason to put drawable_concept or drawable_model inside graphics_surface - you just prevent reuse of something that's potentially useful in other container types...

  • you have some const issues

    • draw should probably be const (and function definitions should not be followed by semicolons ;-)

    • drawable_model(T& item) should take item by const reference

    • push_back(T& drawable) shoudl take drawable by const reference

  • you should use make_shared for exception safety

  • the "factory" functionality would arguably be better off separated into a separate function, rather than buried inside push_back

answered Feb 17, 2014 at 6:56

5 Comments

Good points. Do you think this is a reasonable implementation of Concept Based Polymorphism?
@BlairDavidson: the basic functionality is there... the rest comes down to suiting your actual use(s) and should come out in the wash. There are noteworthy variations - for example, creating run-time polymorphic objects wrapping just a pointer to the concrete type, rather than having the concrete object owned by the wrapper and necessarily heap allocated. Which is optimal depends on the relative lifetimes, whether you're trying to minimise data copying (e.g. concurrent runtime polymorphic access to objects in shared-memory works with the pointer/wrapper).
This question is specifically talking about Sean Parent's work: Concept-based Runtime Polymorphism. The example uses private concept and model classes, though it doesn't explicitly cover the motivation for that choice.
@bames53: I'm confused by your assertion, though not taking issue with it. Do you know / are you Blair - to know what "specifically" the question's talking about? Or are you saying they're an exact match, and/or that nobody else has published anything similar so it's the only candidate for Blair's "I have been reading up"?
@TonyD There's no other candidate, and Blair's code matches the example code given in the linked presentation.
1

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.

answered Feb 17, 2014 at 6:30

2 Comments

Unfortunately, "Concepts" per your answer and the deferred/killed(?) C++0x proposal are a completely different thing from what some people have been calling "Concept Based Polymorphism" - templated factories for capturing specific operations on arbitrary types - which is what the question is about.
Hmm. Yes I agree with your assessment of my answer.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.