Duplication of C / C ++ functions at compile time

If I have a function A(), I’m interested in finding a convenient method for creating a function B()that has the same functionality as that A()that differs only in name. The new feature will be used for one-time use. The goal is to distinguish between calls to the same function in a somewhat primitive sampling profiler, and the duplicated function will be used only in this context. That is, he will never touch the production code and will only be used to mess around.

Suppose first that a macro declares a function with a name Band creates an inline call inside it A(). The problem here is that I don't know a method in GCC to force an arbitrary call to the inline function; it seems that all inline options are for function declarations, not for calls.

There may be some esoteric way to do this using templates, or perhaps a trick to embed a compiler. I am not sure if this is possible. Any suggestions? Unfortunately, the new C ++ standard is not available if that matters.

+3
source share
7 answers

Using patterns

template<int x>
void A()
{
    // ..
}

int main()
{
    A<0>();
    A<1>();
    return 0;
}

Update

A < 0 > A < 1 > . Visual ++ 2010 Release. , . ,

#include <iostream>

template<int x>
void A()
{
    ::std::cout << x << std::endl;
    // ..
}

int main()
{
    A<0>();
    A<1>();
    auto v0 = A<0>;
    auto v1 = A<1>;
    ::std::cout << v0 << std::endl;
    ::std::cout << v1 << std::endl;
    ::std::cout << (v0 == v1) << std::endl;
    return 0;
}
+6

:

#include <iostream>                                                             

template<typename T>
void foo() {
    static int x = 0;
    std::cout << &x << std::endl;
}

int main(int argc, char **argv) {
    foo<int>();
    foo<float>();
    return 0;
}

, , , , . nm .

+3

, :

#define A_CONTENT \
    ... // whatever

void A()
{
    A_CONTENT
}

void B()
{
    A_CONTENT
}

...

A();  // Call to A
B();  // Call to B  

, , , , ?

+2

, , , , . ( , ).

- :

void Func() { .... }

_declspec(noinline) 
void A_Func( return Func(); }
void B_Func( return Func(); }
void C_Func( return Func(); }

, -, .

+2

, , Chromium :

#define CHROMEG_CALLBACK_1(CLASS, RETURN, METHOD, SENDER, ARG1)     \
  static RETURN METHOD ## Thunk(SENDER sender, ARG1 one,            \
                                gpointer userdata) {                \
    return reinterpret_cast<CLASS*>(userdata)->METHOD(sender, one); \
  }                                                                 \
                                                                    \
  virtual RETURN METHOD(SENDER, ARG1);

:

 CHROMEGTK_CALLBACK_1(PageActionViewGtk, gboolean, OnExposeEvent, GdkEventExpose*);

 CHROMEGTK_CALLBACK_1(PageActionViewGtk, gboolean, OnButtonPressed, GdkEventButton*);

- , , . , , . GTK.

+1

, , A , "" , .

, . , .

0

? -, , . , .

++ 11 :

void A() {
    ...
}

...

auto B = [] () -> void { A(); };

Now you can use B syntactically, as if it were a function wrapping A.

0
source

All Articles