我敢肯定这是个坏主意。让我们假装我有充分的理由这样做。我有一棵成功使用静态多态性传递消息的节点树。至关重要的是,每个节点不能知道它连接的节点的类型,它只知道它传递的消息的类型。为了遍历树,我使用 CRTP 实现了访问者模式。这适用于树的第一级。
但是,当遍历到树的第二层时,下一个节点的类型将使用下面的 AnyNode 类擦除。我一直无法弄清楚如何从擦除类型向下转换为具体类型。下面的示例在测试中有效,但我认为它也可能非常危险,只是靠碰巧布置内存的地方工作。
我必须删除 中的访问者类型似乎有问题,AnyNode::Model<T>::acceptDispatch
这在 中是完全已知的AnyNode::Concept::accept
。但我不知道如何从概念向下转换到概念中的模型(我尝试了协变虚cast
函数,但没有奏效)。而且我无法使用虚拟方法将类型化的访问者传递给派生的模型类,因为无法对虚拟方法进行模板化。
有没有一种安全的方式来调用node.accept
和传递访问者而不必删除访问者的类型然后将其静态转换回来?有什么方法可以Model<T>
在运行时将概念向下转换为 a 吗?有没有更好的方法来解决这个问题?是不是有一些疯狂的 C++11 新方法可以解决这个问题,可能是 SFINAE?
class AnyNode
{
struct Concept
{
virtual ~Concept() = default;
template< typename V >
void accept( V & visitor )
{
acceptDispatch( &visitor );
}
virtual void acceptDispatch( VisitorBase * ) = 0;
};
template< typename T >
struct Model : public Concept
{
Model( T &n ) : node( n ) {}
void acceptDispatch( VisitorBase * v ) override
{
// dynamic cast doesn't work, probably for good reason
NodeVisitor< T >* visitor = static_cast< NodeVisitor< T >* >( v );
std::cout << "CAST" << std::endl;
if ( visitor ) {
std::cout << "WAHOO" << std::endl;
node.accept( *visitor );
}
}
private:
T &node;
};
std::unique_ptr< Concept > mConcept;
public:
template< typename T >
AnyNode( T &node ) :
mConcept( new Model< T >( node )) {}
template< typename V >
void accept( V & visitor )
{
mConcept->accept( visitor );
}
};
编辑这里是访问者基类,以及派生访问者的示例。派生的访问者由客户端代码实现(这是库的一部分),因此基类无法知道将实现什么访问者。恐怕这会分散中心问题的注意力,但希望它有助于解释这个问题。此处的所有内容都有效,除非->accept( visitor )
在outlet_visitor::operator()
.
// Base class for anything that implements accept
class Visitable
{
public:
};
// Base class for anything that implements visit
class VisitorBase
{
public:
virtual ~VisitorBase() = default;
};
// Visitor template class
template< typename... T >
class Visitor;
template< typename T >
class Visitor< T > : public VisitorBase
{
public:
virtual void visit( T & ) = 0;
};
template< typename T, typename... Ts >
class Visitor< T, Ts... > : public Visitor< Ts... >
{
public:
using Visitor< Ts... >::visit;
virtual void visit( T & ) = 0;
};
template< class ... T >
class NodeVisitor : public Visitor< T... >
{
public:
};
// Implementation of Visitable for nodes
template< class V >
class VisitableNode : public Visitable
{
template< typename T >
struct outlet_visitor
{
T &visitor;
outlet_visitor( T &v ) : visitor( v ) {}
template< typename To >
void operator()( Outlet< To > &outlet )
{
for ( auto &inlet : outlet.connections()) {
auto n = inlet.get().node();
if ( n != nullptr ) {
// this is where the AnyNode is called, and where the
// main problem is
n->accept( visitor );
}
}
}
};
public:
VisitableNode()
{
auto &_this = static_cast< V & >( *this );
_this.each_in( [&]( auto &i ) {
// This is where the AnyNode is stored on the inlet,
// so it can be retrieved by the `outlet_visitor`
i.setNode( *this );
} );
}
template< typename T >
void accept( T &visitor )
{
auto &_this = static_cast< V & >( *this );
std::cout << "VISITING " << _this.getLabel() << std::endl;
visitor.visit( _this );
// The outlets are a tuple, so we use a templated visitor which
// each_out calls on each member of the tuple using compile-time
// recursion.
outlet_visitor< T > ov( visitor );
_this.each_out( ov );
}
};
// Example instantiation of `NodeVistor< T... >`
class V : public NodeVisitor< Int_IONode, IntString_IONode > {
public:
void visit( Int_IONode &n ) {
cout << "Int_IONode " << n.getLabel() << endl;
visited.push_back( n.getLabel());
}
void visit( IntString_IONode &n ) {
cout << "IntString_IONode " << n.getLabel() << endl;
visited.push_back( n.getLabel());
}
std::vector< std::string > visited;
};