投稿数 - 437, コメント - 52722, トラックバック - 156

超汎用関数ポインタ

C++

某所でデリゲートと関数ポインタが話題になっているで、ここでは boost.function を。

boost.function はありとあらゆる関数表現を統一して扱えるというもの。boost.function がなければ、かなり厄介な作業を強いられるだろう(特に boost.bind と boost.lambda の返値は凶悪なので)。

function<bool (int, int)>

と書けば、int の引数を 2 つ持ち、bool を返す関数「のようなもの」は何でも受け入れる事ができる。古いコンパイラでは「bool (int, int)」と書けないので、他の手段を用いなければならないが、古いコンパイラなんて誰も使わないよね。

以下は、引数が偶数かどうかを返す関数(のようなもの)を扱った例。

#include <algorithm>
#include <boost/bind.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/function.hpp>

using namespace std;
using namespace boost;

template <typename T>
bool is_even(const T& x){
    return (x % 2 == 0);
}

template <typename T>
struct even{
    bool operator()(const T& x) const{
        return (x % 2 == 0);
    }

    bool is_even(const T& x) const{
        return (x % 2 == 0);
    }
};

int main(){
    // 普通の関数ポインタ
    function<bool (int)> f1 = is_even<int>;

    // 関数オブジェクト
    function<bool (int)> f2 = even<int>();

    // メンバ関数(のインスタンスを束縛した関数)
    even<int> e;
    function<bool (int)> f3 = bind(&even<int>::is_even, &e, ::_1);

    // メンバ関数
    function<bool (even<int>*, int)> fe = &even<int>::is_even;
    // メンバ関数(のインスタンスを束縛した関数その 2)
    function<bool (int)> f4 = bind(fe, &e, ::_1);

    // ラムダ
    function<bool (int)> f5 = lambda::_1 % 2 == 0;


    // わざわざ function を使う必要はないけど、使用例
    int a[] = {1, 2, 3, 4, 7};

    // f1 の部分は f2~f5 に置き換え可能
    int count = count_if(a, sizeof(a) / sizeof(a[0]), f1);
    cout << count << endl;

    return 0;
}

投稿日時 : 2007年10月26日 18:50

フィードバック

# re: 超汎用関数ポインタ

とてもコメントしたいのですが、くやしながら私が理解できるレベルを超えています・・・。
もうちょっと勉強して出直してきたいと思います・・・。
2007/10/26 21:25 | ながせ

# re: 超汎用関数ポインタ

一見難しそうに見えますが、実際は単純ですよー(使うだけなら^^; 中身は難しくてよくわかりません)。
2007/10/26 22:13 | 囚人

# re: 超汎用関数ポインタ

> 古いコンパイラでは「bool (int, int)」と書けないので、他の手段を用いなければならないが
typedefで別名付ければおK?ってこと?
2007/10/26 23:14 | かずくん

# re: 超汎用関数ポインタ

いや、テンプレートパラメータに()が入る事が解釈できないのか、
引数1個の場合は
function1<bool, int>
2個の場合は
function2<bool, int, int>
みたいなぶっ細工な感じにしないといけません。
2007/10/26 23:21 | 囚人

# re: 超汎用関数ポインタ

functionはまだ使ったことなかったなー
おもしろそーだからボクもそのうち作ろう
2007/10/27 1:37 | アキラ

# 環境がすぐ整うってうれしいよね

環境がすぐ整うってうれしいよね
2007/10/27 9:02 | ながせろぐ

# 環境がすぐ整うってうれしいよね

環境がすぐ整うってうれしいよね
2007/10/27 9:02 | ながせろぐ

# re: 超汎用関数ポインタ

>functionはまだ使ったことなかったなー
>おもしろそーだからボクもそのうち作ろう

アキラさんは実際作って理解を深めるというタイプなんですよね。私もマネしてみようかな。
2007/10/27 23:30 | 囚人

# re: 超汎用関数ポインタ

実際作ってみると、Boostで使われてるテクニックが身につくので楽しいですよ
2007/10/28 13:08 | アキラ

# re: 超汎用関数ポインタ

上記コードがコンパイル通らなかったので念のため報告です。

#include <iostream>追加


>count_if(a, sizeof(a) / sizeof(a[0]), f1);

count_if(a, a + sizeof(a) / sizeof(a[0]), f1);
2007/10/29 13:34 | アキラ

# re: 超汎用関数ポインタ

あと、私の環境(VC++8.0)だとこのコードがアプリケーションエラーになるので

>function<bool (int)> f1 = is_even<int>;

これで直りました
function<bool (int)> f1 = &is_even<int>;
2007/10/29 13:45 | アキラ

# re: 超汎用関数ポインタ

>count_if(a, a + sizeof(a) / sizeof(a[0]), f1);

あっひゃ~。やっちゃった。ありがとうございます。


私が試したコンパイラが古かったからかな。VC++7.1 だと
>function<bool (int)> f1 = &is_even<int>;
が逆にアプリケーションエラーになっちゃいました。
2007/10/29 14:47 | 囚人

# re: 超汎用関数ポインタ

>VC++7.1 だと

あら…7.1持ってないのでそのへんはわからないですね
2007/10/29 19:45 | アキラ

# delegateもどき

delegateもどき
2007/12/30 0:22 | 東方算程譚

# delegateもどき

delegateもどき
2007/12/30 0:23 | 東方算程譚

# re: 超汎用関数ポインタ

>実際作ってみると、Boostで使われてるテクニックが身につくので楽しいですよ

boost::functionは難しくてギブアップ・・・
可変長のオーバーロードをどうやって解決しているのか
が知りたかったんだけど、
どういう仕組みなのか全然分かりません・・・
分かっている人がいたら教えて頂きたいです。
BOOST_FUNCTION_PARMSとか、
BOOST_FUNCTION_ARGSのからくり?
関数オブジェクトとか渡して引数の数がいくつでも
ちゃんと呼び出されるようだけど、
まるで自分にはマジックなようにすら見える・・・
もしかしてboostってプリプロセッサを拡張している?、、

ていうか、自分のプリプロセッサの知識不足なだけなんですかね?どうなんでしょう。
2008/01/04 17:35 | 通りすがり

# re: 超汎用関数ポインタ

>通りすがりさん

新しい記事にしました。
2008/01/08 0:31 | 囚人

# axqKQgYrXDiAZe

If you are going to watch comical videos on the net then I suggest you to go to see this web site, it carries truly therefore comical not only video clips but also extra stuff.

# Very good article. I'm facing some of these issues as well..

Very good article. I'm facing some of these issues as well..

# Very good article. I'm facing some of these issues as well..

Very good article. I'm facing some of these issues as well..

# Very good article. I'm facing some of these issues as well..

Very good article. I'm facing some of these issues as well..

# Very good article. I'm facing some of these issues as well..

Very good article. I'm facing some of these issues as well..

# Its such as you read my thoughts! You seem to grasp so much about this, such as you wrote the e book in it or something. I feel that you just could do with some percent to drive the message home a little bit, but instead of that, this is wonderful blog

Its such as you read my thoughts! You seem to grasp so much about this, such as you wrote the e book in it or something.
I feel that you just could do with some percent to drive
the message home a little bit, but instead of that,
this is wonderful blog. A fantastic read. I'll certainly be back.

# Its such as you read my thoughts! You seem to grasp so much about this, such as you wrote the e book in it or something. I feel that you just could do with some percent to drive the message home a little bit, but instead of that, this is wonderful blog

Its such as you read my thoughts! You seem to grasp so much about this, such as you wrote the e book in it or something.
I feel that you just could do with some percent to drive
the message home a little bit, but instead of that,
this is wonderful blog. A fantastic read. I'll certainly be back.

# Its such as you read my thoughts! You seem to grasp so much about this, such as you wrote the e book in it or something. I feel that you just could do with some percent to drive the message home a little bit, but instead of that, this is wonderful blog

Its such as you read my thoughts! You seem to grasp so much about this, such as you wrote the e book in it or something.
I feel that you just could do with some percent to drive
the message home a little bit, but instead of that,
this is wonderful blog. A fantastic read. I'll certainly be back.

# Hi, the whole thing is going perfectly here and ofcourse every one is sharing facts, that's really excellent, keep up writing.

Hi, the whole thing is going perfectly here and ofcourse every one is sharing facts, that's really excellent,
keep up writing.

# Hi, the whole thing is going perfectly here and ofcourse every one is sharing facts, that's really excellent, keep up writing.

Hi, the whole thing is going perfectly here and ofcourse every one is sharing facts, that's really excellent,
keep up writing.

# Hi, the whole thing is going perfectly here and ofcourse every one is sharing facts, that's really excellent, keep up writing.

Hi, the whole thing is going perfectly here and ofcourse every one is sharing facts, that's really excellent,
keep up writing.

# Hi, the whole thing is going perfectly here and ofcourse every one is sharing facts, that's really excellent, keep up writing.

Hi, the whole thing is going perfectly here and ofcourse every one is sharing facts, that's really excellent,
keep up writing.

# Thanks , I've recently been searching for info about this subject for a while and yours is the greatest I have discovered so far. However, what concerning the bottom line? Are you positive about the supply? quest bars http://j.mp/3C2tkMR quest bars

Thanks , I've recently been searching for info about this subject for
a while and yours is the greatest I have discovered so far.

However, what concerning the bottom line? Are you positive about the supply?
quest bars http://j.mp/3C2tkMR quest bars

# You have made some really good points there. I looked on the net to find out more about the issue and found most individuals will go along with your views on this site. quest bars https://www.iherb.com/search?kw=quest%20bars quest bars

You have made some really good points there. I looked on the net
to find out more about the issue and found most individuals will go along with your views on this site.
quest bars https://www.iherb.com/search?kw=quest%20bars quest bars

# Asking questions are genuinely pleasant thing if you are not understanding something entirely, however this article presents fastidious understanding even. scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery

Asking questions are genuinely pleasant thing if you are
not understanding something entirely, however this article presents fastidious
understanding even. scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery

# Asking questions are genuinely pleasant thing if you are not understanding something entirely, however this article presents fastidious understanding even. scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery

Asking questions are genuinely pleasant thing if you are
not understanding something entirely, however this article presents fastidious
understanding even. scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery

# Asking questions are genuinely pleasant thing if you are not understanding something entirely, however this article presents fastidious understanding even. scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery

Asking questions are genuinely pleasant thing if you are
not understanding something entirely, however this article presents fastidious
understanding even. scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery

# Asking questions are genuinely pleasant thing if you are not understanding something entirely, however this article presents fastidious understanding even. scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery

Asking questions are genuinely pleasant thing if you are
not understanding something entirely, however this article presents fastidious
understanding even. scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery

# I visited multiple sites except the audio feature for audio songs current at this website is in fact wonderful.

I visited multiple sites except the audio feature
for audio songs current at this website is in fact wonderful.

# I visited multiple sites except the audio feature for audio songs current at this website is in fact wonderful.

I visited multiple sites except the audio feature
for audio songs current at this website is in fact wonderful.

# I visited multiple sites except the audio feature for audio songs current at this website is in fact wonderful.

I visited multiple sites except the audio feature
for audio songs current at this website is in fact wonderful.

# I visited multiple sites except the audio feature for audio songs current at this website is in fact wonderful.

I visited multiple sites except the audio feature
for audio songs current at this website is in fact wonderful.

# I think that is one of the such a lot significant info for me. And i am satisfied reading your article. However should commentary on few general issues, The site taste is perfect, the articles is in reality great : D. Just right task, cheers

I think that is one of the such a lot significant info for me.
And i am satisfied reading your article. However should commentary on few general issues, The site taste is perfect, the articles is in reality great : D.

Just right task, cheers

# I think that is one of the such a lot significant info for me. And i am satisfied reading your article. However should commentary on few general issues, The site taste is perfect, the articles is in reality great : D. Just right task, cheers

I think that is one of the such a lot significant info for me.
And i am satisfied reading your article. However should commentary on few general issues, The site taste is perfect, the articles is in reality great : D.

Just right task, cheers

# I think that is one of the such a lot significant info for me. And i am satisfied reading your article. However should commentary on few general issues, The site taste is perfect, the articles is in reality great : D. Just right task, cheers

I think that is one of the such a lot significant info for me.
And i am satisfied reading your article. However should commentary on few general issues, The site taste is perfect, the articles is in reality great : D.

Just right task, cheers

# I think that is one of the such a lot significant info for me. And i am satisfied reading your article. However should commentary on few general issues, The site taste is perfect, the articles is in reality great : D. Just right task, cheers

I think that is one of the such a lot significant info for me.
And i am satisfied reading your article. However should commentary on few general issues, The site taste is perfect, the articles is in reality great : D.

Just right task, cheers

# VsdVzFMqIsUvqm

http://imrdsoacha.gov.co/silvitra-120mg-qrms
2022/04/19 13:29 | johnanz

# Drug information. Get information now.
https://edonlinefast.com
Get here. What side effects can this medication cause?

Drug information. Get information now.
https://edonlinefast.com
Get here. What side effects can this medication cause?
2023/02/17 6:54 | EdPills

# Cautions. Prescription Drug Information, Interactions & Side.
https://edonlinefast.com
Best and news about drug. Best and news about drug.

Cautions. Prescription Drug Information, Interactions & Side.
https://edonlinefast.com
Best and news about drug. Best and news about drug.
2023/02/17 11:02 | EdOnline

# Everything information about medication. All trends of medicament.
https://edonlinefast.com
Drug information. Learn about the side effects, dosages, and interactions.

Everything information about medication. All trends of medicament.
https://edonlinefast.com
Drug information. Learn about the side effects, dosages, and interactions.
2023/02/18 15:16 | EdOnline

# Generic Name. Get warning information here.
https://canadianfast.com/
drug information and news for professionals and consumers. Long-Term Effects.

Generic Name. Get warning information here.
https://canadianfast.com/
drug information and news for professionals and consumers. Long-Term Effects.
2023/02/20 16:49 | CanadaBest

# prednisone 54899 - https://prednisonesale.pro/#

prednisone 54899 - https://prednisonesale.pro/#
2023/04/22 15:08 | Prednisone

# over the counter eczema cream https://overthecounter.pro/#

over the counter eczema cream https://overthecounter.pro/#
2023/05/08 22:43 | OtcJikoliuj

# natural ed remedies: https://edpills.pro/#

natural ed remedies: https://edpills.pro/#
2023/05/16 3:17 | EdPillsPro

# best non prescription ed pills https://edpill.pro/# - erection pills

best non prescription ed pills https://edpill.pro/# - erection pills
2023/06/27 14:35 | EdPills

# prescription prices comparison https://fastpills.pro/# list of trusted canadian pharmacies

prescription prices comparison https://fastpills.pro/# list of trusted canadian pharmacies
2023/06/29 23:10 | FastPills

# Paxlovid buy online https://paxlovid.store/
buy paxlovid online

Paxlovid buy online https://paxlovid.store/
buy paxlovid online
2023/07/13 21:41 | Paxlovid

# paxlovid for sale https://paxlovid.life/# Paxlovid buy online

paxlovid for sale https://paxlovid.life/# Paxlovid buy online
2023/07/26 6:15 | Paxlovid

# online ed medications https://edpills.ink/# - men's ed pills

online ed medications https://edpills.ink/# - men's ed pills
2023/07/27 0:49 | EdPills

# versandapotheke deutschland

https://onlineapotheke.tech/# online apotheke preisvergleich
versandapotheke
2023/09/26 14:01 | Williamreomo

# п»їonline apotheke

http://onlineapotheke.tech/# versandapotheke deutschland
versandapotheke
2023/09/26 23:56 | Williamreomo

# internet apotheke

https://onlineapotheke.tech/# online apotheke versandkostenfrei
online apotheke gГ?nstig
2023/09/27 1:16 | Williamreomo

# online apotheke gГјnstig

http://onlineapotheke.tech/# gГ?nstige online apotheke
versandapotheke
2023/09/27 4:37 | Williamreomo

# online apotheke deutschland

http://onlineapotheke.tech/# online apotheke gГ?nstig
п»?online apotheke
2023/09/27 5:02 | Williamreomo

# gГјnstige online apotheke

http://onlineapotheke.tech/# versandapotheke deutschland
online apotheke preisvergleich
2023/09/27 7:37 | Williamreomo

# п»їonline apotheke

http://onlineapotheke.tech/# gГ?nstige online apotheke
online apotheke preisvergleich
2023/09/27 8:24 | Williamreomo

# п»їonline apotheke

https://onlineapotheke.tech/# online apotheke deutschland
versandapotheke
2023/09/27 11:36 | Williamreomo

# comprare farmaci online con ricetta

acheter sildenafil 100mg sans ordonnance
2023/09/27 18:59 | Rickeyrof

# farmacia online migliore

acheter sildenafil 100mg sans ordonnance
2023/09/27 20:37 | Rickeyrof

# п»їfarmacia online migliore

acheter sildenafil 100mg sans ordonnance
2023/09/27 21:26 | Rickeyrof

# best male ed pills https://edpillsotc.store/# - ed pills otc

best male ed pills https://edpillsotc.store/# - ed pills otc
2023/10/08 1:09 | EdPills

# approved canadian pharmacies

I always find great deals in their monthly promotions. http://mexicanpharmonline.shop/# reputable mexican pharmacies online
2023/10/16 22:48 | Dannyhealm

# citrus ortho and joint institute

The staff ensures a seamless experience every time. http://mexicanpharmonline.shop/# pharmacies in mexico that ship to usa
2023/10/16 23:59 | Dannyhealm

# canada rx prices

Leading with integrity on the international front. http://mexicanpharmonline.shop/# reputable mexican pharmacies online
2023/10/17 7:57 | Dannyhealm

# canada on line pharmacies

Their international shipment tracking system is top-notch. http://mexicanpharmonline.shop/# mexico drug stores pharmacies
2023/10/18 0:23 | Dannyhealm

# no presciption needed

They simplify global healthcare. http://mexicanpharmonline.com/# mexican pharmaceuticals online
2023/10/18 3:14 | Dannyhealm

# rx mexico online

Get here. https://mexicanpharmonline.shop/# mexican border pharmacies shipping to usa
2023/10/18 5:28 | Dannyhealm

# mexico drug stores online

Love the seasonal health tips they offer. http://mexicanpharmonline.com/# mexico drug stores pharmacies
2023/10/18 9:34 | Dannyhealm

# canada pharmacies online prescriptions

Always greeted with warmth and professionalism. https://mexicanpharmonline.shop/# mexican pharmaceuticals online
2023/10/18 18:53 | Dannyhealm

# online prescription canada

They understand the intricacies of international drug regulations. https://mexicanpharmonline.com/# mexican mail order pharmacies
2023/10/19 3:30 | Dannyhealm

# erectile dysfunction medications

https://tadalafil.trade/# cheapest tadalafil india
2023/11/22 16:05 | WilliamApomb

# farmacia online migliore https://farmaciait.pro/ farmacie online autorizzate elenco

farmacia online migliore https://farmaciait.pro/ farmacie online autorizzate elenco
2023/12/04 10:12 | Farmacia

# Sildenafil Preis

https://apotheke.company/# gГ?nstige online apotheke
2023/12/18 8:20 | StevenNuant

# cheapest ed pills online https://edpills.tech/# ed meds

cheapest ed pills online https://edpills.tech/# ed meds
2023/12/23 8:09 | EdPills

コメントの投稿

タイトル  
名前  
URL
コメント