3

for異なる関数を同じループに埋め込む必要があるプログラムを C++ で作成しています。

例:

for(i = 0; i < N_ITER; i++) {
    /* ... */
    function_A(); 
    /* ... */
}

for(i = 0; i < N_ITER; i++) {
    /* ... */
    function_B(); 
    /* ... */
}

パフォーマンスを考慮して、この関数にインライン化する必要がありfunction_Aます。function_B私の最初の解決策は、次のようにファンクターと関数テンプレートを使用することです。

class function_A {
public:
    void operator()() {
        /* ... */   
    }
};

class function_B {
public:
    void operator()() {
        /* ... */
    }
};

template <class T>
class run {
public:
    void operator()() {
        /* ... */
        T func;
        for (i = 0; i < N_ITER; i++) {
            /* ... */
            func();
            /* ... */
        }
        /* ... */
    }
};

そして、次のような関数を呼び出すことができます:

run<function_A>()();
run<function_B>()();

class xxx { public: void operator()() {...} };しかしすぐに、コード内に重複したファンクター定義が多すぎて、ぎこちなく見えることがわかりました。


だから私はラムダを使って解決策になりました:

auto function_A = []()
{
    /* ... */
};

auto function_B = []()
{
    /* ... */
};

template <class T>
class run {
public:
    T func;
    run(T func) : func(func) {}
    void operator()() {
        /* ... */
        for (i = 0; i < N_ITER; i++) {
            /* ... */
            func();
            /* ... */
        }
        /* ... */
    }
};

しかし、今回はこれらの関数を呼び出すのが難しくなります:

run<decltype(function_A)> funcA(function_A); funcA();
run<decltype(function_A)> funcB(function_A); funcB();

そして、それは前の解決策ほど明確ではありません。


C ++で実装するよりエレガントな方法はありますか? 何か不足していますか?任意の提案をいただければ幸いです!!!

4

2 に答える 2

2

これを試して:

void run(std::function<void(void)> fn)
{
    for (int i = 0; i < N_ITER; i++)
        fn();
}

(...)

auto lambda1 = []()
    {
        std::cout << "Hello, world!\n";
    };

run(lambda1);

そのような関数/メソッドを準備した後run、これを行うこともできます:

class C
{
public:
    void operator()()
    {
        printf("Hello world from a functor!\n");
    }
};

(...)

C c;

run(c);

編集:コメントに応じて:

以前のソリューションはインライン化されていませんでした。ただし、これは次のとおりです。

template <typename T>
void run2(T fn)
{
    for (int i = 0; i < 10; i++)
        fn();
}

int main(int argc, char * argv[])
{
    getchar();

    auto lambda1 = []()
        {
            std::cout << "Hello, world!\n";
        };

    run2(lambda1);

    getchar();
}

分解:

        auto lambda1 = []()
            {
                std::cout << "Hello, world!\n";
            };

        run2(lambda1);
002E1280  mov         ecx,dword ptr ds:[2E3044h]  
002E1286  call        std::operator<<<std::char_traits<char> > (02E1730h)  
002E128B  dec         esi  
002E128C  jne         main+10h (02E1280h)

実際には、run2 の場合のファンクターを使用したソリューションもインライン化されていましたが、少し長くなりました。

    C c;

    run2(c);
01031293  mov         esi,0Ah  
01031298  jmp         main+30h (010312A0h)  
0103129A  lea         ebx,[ebx]  
010312A0  mov         ecx,dword ptr ds:[1033044h]                          \
010312A6  mov         edx,10331ACh                                         |
010312AB  call        std::operator<<<std::char_traits<char> > (01031750h)  > loop 
010312B0  dec         esi                                                  |
010312B1  jne         main+30h (010312A0h)                                 /
于 2013-06-21T06:54:54.253 に答える
1

Regarding your concern

But soon I found that there's too much duplicated functor definations class xxx { public: void operator()() {...} }; in code and it looks awkward.

you should probably not be writing

class function_B {
public:
    void operator()() {
        /* ... */
    }
};

but the version with struct where you save one line of syntactic clutter (public:) because members of structs are by default public:

struct function_B {
    void operator()() {
        /* ... */
    }
};

which really, IMO, has rather little overhead compared to a simple function declaration.

Of course, the lambda version has a little less, and combined with a free templeate function, you also don't need decltype.

Wrt. to the lambda solution, one also must take into account that you can put the whole definition of the struct into a header file, while you cannot put the definition of the lambda object in a header file, if that header file is used from multiple translation units.

Of course the problem as stated rather looks like these definition would most likely be written inside a cpp file, so the lambda solution probably would works just as well.

于 2013-06-21T07:50:20.797 に答える