[cfe-dev] Is it safe to cast-away constness in RecursiveASTVisitors?

Roman Popov via cfe-dev cfe-dev at lists.llvm.org
Thu Jun 28 05:52:57 PDT 2018


Hi all,

I want to implement RecursiveASTVisitor to find certain nodes in AST.  This
class should not modify AST, so I want to make a search method to be const:

bool MyCallSearch::FindMyCall (const clang::Stmt *rootStmt ) const {
     this->TraverseStmt(stmt);
}

Unfortunately,  RecursiveASTVisitor methods are non-const and accept
non-const pointers to AST nodes. So I wonder, is it safe to cast away
constness like this ? :

bool MyCallSearch::FindMyCall (const clang::Stmt *rootStmt ) const {
     const_cast<MyCallSearch*>(this)->TraverseStmt(const_cast<clang::Stmt
*>(stmt));

}

Thanks,
Roman
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20180628/0781b61b/attachment.html>


More information about the cfe-dev mailing list