Note that there are some explanatory texts on larger screens.

plurals
  1. POC callback functions defined in an unnamed namespace?
    text
    copied!<p>I have a C++ project that uses a C bison parser. The C parser uses a struct of function pointers to call functions that create proper AST nodes when productions are reduced by bison:</p> <pre><code>typedef void Node; struct Actions { Node *(*newIntLit)(int val); Node *(*newAsgnExpr)(Node *left, Node *right); /* ... */ }; </code></pre> <p>Now, in the C++ part of the project, i fill those pointers</p> <pre><code>class AstNode { /* ... */ }; class IntLit : public AstNode { /* ... */ }; extern "C" { Node *newIntLit(int val) { return (Node*)new IntLit(val); } /* ... */ } Actions createActions() { Actions a; a.newIntLit = &amp;newIntLit; /* ... */ return a; } </code></pre> <p>Now the only reason i put them within <code>extern "C"</code> is because i want them to have C calling conventions. But optimally, i would like their names still be mangled. They are never called by-name from C code, so name mangling isn't an issue. Having them mangled will avoid name conflicts, since some actions are called like <code>error</code>, and the C++ callback function has ugly names like the following just to avoid name clashes with other modules. </p> <pre><code>extern "C" { void uglyNameError(char const *str) { /* ... */ } /* ... */ } a.error = &amp;uglyNameError; </code></pre> <p>I wondered whether it could be possible by merely giving the function type C linkage</p> <pre><code>extern "C" void fty(char const *str); namespace { fty error; /* Declared! But i can i define it with that type!? */ } </code></pre> <p>Any ideas? I'm looking for Standard-C++ solutions. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload