This is kind of like doing aspect oriented programming with an **after* specification. AspectC++ exists as a research project, but I don't think it is active or will work with modern C++(14).
Our DMS Software Reengineering Toolkit with its C++14 Front End can probably do this.
(Some people imagine that parsing C++ is easy. It is not.)
DMS parses C++ to ASTs, allows you to apply source-to-source transformations to modify those ASTs, and then can regenerate valid C++ text from the modified trees, including preserving comments.
The source-to-source transformations are written in terms of C++ source-level syntax, and look like this:
if you see *this*, replace it by *that* when *somecondition*
where this and that are source patterns with placeholder variables, and somecondition checks that the transformation should be applied.
DMS can/does build symbol tables for the source code. Often the somecondition checks that a symbol of interest has the right properties to trigger a rule.
In OP's case, he wants a rule something like:
rule insert_into_constructor(q:qualified_path, ss: statement_sequence):
statement_sequence -> statement_sequence
= " \q () { \ss } " -- left hand side pattern *this*
-> " \q () { \ss \mycode\(\) } " -- right hand side pattern *that*
if is_in_namespace("foo", q); -- *when* condition
\mycode can be inlined C++ syntax, or written as follows:
pattern my_code(): statement_sequence =
" <arbitrayC++code> " ;
The metaquotes " are used to seperate the syntax of the rule language from the syntax of ++ code. The \n you see inside the metaquotes are placeholder variables, that represent the syntactic structure declared for n. A placeholder in the left pattern of a rule is matched against code in the program; the same placeholder on the right hand side pattern (after ->) is replaced by what got matched in the left hand side. The is_in_namespace metafunction checks that a found constructor is in the namespace that you want to modify. You might want a different check here; this is intended only to be suggestive. Note that the matched metavariable q is passed to the when condition; it doesn't need a "\" because it isn't inside metaquotes.
So, you can use DMS to implement aspect-oriented programming. It isn't limited to that.