<p>I'm sorry if my question is so long and technical but I think it's so important other people will be interested about it</p>

<p>I was looking for a way to separate clearly some softwares internals from their representation in c++</p>

<p>I have a generic parameter class (to be later stored in a container) that can contain any kind of value with the the boost::any class</p>

<p>I have a base class (roughly) of this kind (of course there is more stuff)</p>

<pre><code>class Parameter 
{
public:
    Parameter()
    template typename&lt;T&gt; T GetValue() const { return any_cast&lt;T&gt;( _value ); }
    template typename&lt;T&gt; void SetValue(const T&amp; value) { _value = value; }
    string GetValueAsString() const = 0;
    void SetValueFromString(const string&amp; str) const = 0;
private:
    boost::any _value;
}
</code></pre>

<p>There are two levels of derived classes:
The first level defines the type and the conversion to/from string (for example ParameterInt or ParameterString)
The second level defines the behaviour and the real creators (for example deriving ParameterAnyInt and ParameterLimitedInt from ParameterInt or ParameterFilename from GenericString)</p>

<p>Depending on the real type I would like to add external function or classes that operates depending on the specific parameter type without adding virtual methods to the base class and without doing strange casts</p>

<p>For example I would like to create the proper gui controls depending on parameter types:</p>

<pre><code>Widget* CreateWidget(const Parameter&amp; p)
</code></pre>

<p>Of course I cannot understand real Parameter type from this unless I use RTTI or implement it my self (with enum and switch case), but this is not the right OOP design solution, you know.</p>

<p>The classical solution is the Visitor design pattern <a href="http://en.wikipedia.org/wiki/Visitor_pattern" rel="nofollow noreferrer">http://en.wikipedia.org/wiki/Visitor_pattern</a></p>

<p>The problem with this pattern is that I have to know in advance which derived types will be implemented, so (putting together what is written in wikipedia and my code) we'll have sort of: </p>

<pre><code>struct Visitor 
{
  virtual void visit(ParameterLimitedInt&amp; wheel) = 0;
  virtual void visit(ParameterAnyInt&amp; engine) = 0;
  virtual void visit(ParameterFilename&amp; body) = 0;
};
</code></pre>

<p>Is there any solution to obtain this behaviour in any other way without need to know in advance all the concrete types and without deriving the original visitor?</p>

<hr>

<p><strong>Edit:</strong> <a href="https://stackoverflow.com/q/31913">Dr. Pizza's solution seems the closest to what I was thinking</a>, but the problem is still the same and the method is actually relying on dynamic_cast, that I was trying to avoid as a kind of (even if weak) RTTI method</p>

<p>Maybe it is better to think to some solution without even citing the visitor Pattern and clean our mind. The purpose is just having the function such:</p>

<pre><code>Widget* CreateWidget(const Parameter&amp; p)
</code></pre>

<p>behave differently for each "concrete" parameter without losing info on its type </p>

[{'answer_id': 31944, 'author': 'graham.reeds', 'author_id': 342, 'author_profile': 'https://Stackoverflow.com/users/342', 'pm_score': 0, 'selected': False, 'text': '<p>If I understand this correctly...</p>\n\n<p>We had a object that could use different hardware options. To facilitate this we used a abstract interface of Device. Device had a bunch of functions that would be fired on certain events. The use would be the same but the various implementations of the Device would either have a fully-fleshed out functions or just return immediately. To make life even easier, the functions were void and threw exceptions on when something went wrong.</p>\n'}, {'answer_id': 31953, 'author': 'DrPizza', 'author_id': 2131, 'author_profile': 'https://Stackoverflow.com/users/2131', 'pm_score': 1, 'selected': False, 'text': '<p>I\'ve used <a href="http://www.objectmentor.com/resources/articles/acv.pdf" rel="nofollow noreferrer">this</a> ("acyclic visitor") to good effect; it makes adding new classes to the hierarchy possible without changing existing ones, to some extent.</p>\n'}, {'answer_id': 31999, 'author': 'Konrad Rudolph', 'author_id': 1968, 'author_profile': 'https://Stackoverflow.com/users/1968', 'pm_score': 0, 'selected': False, 'text': '<p>For completeness\'s sake:</p>\n\n<p>it\'s of course completely possible to write an own implementation of a multimethod pointer table for your objects and calculate the method addresses manually at run time. There\'s a <a href="http://www.research.att.com/~bs/multimethods.pdf" rel="nofollow noreferrer">paper</a> by Stroustrup on the topic of implementing multimethods (albeit in the compiler).</p>\n\n<p>I wouldn\'t really advise anyone to do this. Getting the implementation to perform well is quite complicated and the syntax for using it will probably be very awkward and error-prone. If everything else fails, this might still be the way to go, though.</p>\n'}, {'answer_id': 33452, 'author': 'genix', 'author_id': 2714, 'author_profile': 'https://Stackoverflow.com/users/2714', 'pm_score': 0, 'selected': False, 'text': '<p>I am having trouble understanding your requirements. But Ill state - in my own words as it were - what I understand the situation to be:</p>\n\n<ul>\n<li><p>You have abstract Parameter class, which is subclassed eventually to some concrete classes (eg: ParameterLimitedInt).</p></li>\n<li><p>You have a seperate GUI system which will be passed these parameters in a generic fashion, but the catch is that it needs to present the GUI component specific to the concrete type of the parameter class.</p></li>\n<li><p>The restrictions are that you dont want to do RTTID, and dont want to write code to handle every possible type of concrete parameter.</p></li>\n<li><p>You are open to using the visitor pattern.</p></li>\n</ul>\n\n<p>With those being your requirements, here is how I would handle such a situation:</p>\n\n<p>I would implement the visitor pattern where the accept() returns a boolean value. The base Parameter class would implement a virtual accept() function and return false.</p>\n\n<p>Concrete implementations of the Parameter class would then contain accept() functions which will call the visitor\'s visit(). They would return true.</p>\n\n<p>The visitor class would make use of a templated visit() function so you would only override for the concrete Parameter types you care to support:</p>\n\n<pre><code>class Visitor\n{\npublic:\n  template&lt; class T &gt; void visit( const T&amp; param ) const\n  {\n    assert( false &amp;&amp; "this parameter type not specialised in the visitor" );\n  }\n  void visit( const ParameterLimitedInt&amp; ) const; // specialised implementations...\n}\n</code></pre>\n\n<p>Thus if accept() returns false, you know the concrete type for the Parameter has not implemented the visitor pattern yet (in case there is additional logic you would prefer to handle on a case by case basis). If the assert() in the visitor pattern triggers, its because its not visiting a Parameter type which you\'ve implemented a specialisation for.</p>\n\n<p>One downside to all of this is that unsupported visits are only caught at runtime.</p>\n'}, {'answer_id': 33688, 'author': 'Josh', 'author_id': 3521, 'author_profile': 'https://Stackoverflow.com/users/3521', 'pm_score': 2, 'selected': False, 'text': '<p>For a generic implementation of <a href="http://en.wikipedia.org/wiki/Visitor_pattern" rel="nofollow noreferrer">Vistor</a>, I\'d suggest the <a href="http://loki-lib.sourceforge.net/index.php?n=Pattern.Visitor" rel="nofollow noreferrer">Loki Visitor</a>, part of the <a href="http://loki-lib.sourceforge.net/index.php?n=Main.HomePage" rel="nofollow noreferrer">Loki library</a>.</p>\n'}]
['https://Stackoverflow.com/questions/31913', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/3373/']