はみ出しRAPTの戯言

- a day after yesterday -

目次

Blog 利用状況

ニュース

書庫

日記カテゴリ

コミュニティ リンク

マクロで可変長引数を使う方法

■はじめに

C++では、関数引数に func( LPCTSTR lpszFormat, ... ); などと書くことによって
可変長引数を扱うことができるが、マクロではできない。

マクロで可変長引数を使えたら便利なのになー、ということで、Tips を紹介する。
それはズバリ、クラスを利用するのである。クラスとマクロの複合技で実現する。

 

■イメージ

例えば、下記のようなファイルへ出力するマクロがあったとする。

#define PUTLOG1( fp, format, arg1 ) \
    _ftprintf( fp, _T( "[%s](%d): " ) format _T( "\n" ), __FILE__, __LINE__, arg1 )
#define PUTLOG2( fp, format, arg1, arg2 ) \
    _ftprintf( fp, _T( "[%s](%d): " ) format _T( "\n" ), __FILE__, __LINE__, arg1, arg2 )
#define PUTLOG3( fp, format, arg1, arg2, arg 3 ) \
    _ftprintf( fp, _T( "[%s](%d): " ) format _T( "\n" ), __FILE__, __LINE__, arg1, arg2, arg3 )

 

これを下記のように1つのマクロで書けないか、というオハナシ。

#define PUTLOG( fp, format, ... ) /* how to */

 

■ソースコード

// ※下記のコードは VC++ .NET2003 で動作を確認。
class CPutLog
{
protected:
    const char* m_lpszFile;
    int m_nLine;

public:
    CPutLog( const char* lpszFile, int nLine )
        : m_lpszFile( lpszFile ), m_nLine( nLine )
    {
    }

    void operator()( FILE* fp, LPCTSTR lpszFormat, ... ) const
    {
        va_list ptr;
        va_start( ptr, lpszFormat );
        fprintf( fp, "[%s](%d): ", m_lpszFile, m_nLine );
        _vftprintf( fp, lpszFormat, ptr );
        fprintf( fp, "%c", '\n' );
        va_end( ptr );
    }
};

#define PUTLOG    CPutLog( __FILE__, __LINE__ )

 


これで OK! あとは使うダケ。

PUTLOG( fp, _T( "%s" ), _T( "これはテストです" ) );
PUTLOG( fp, _T( "%d + %d = %d" ), 1, 3, 4 );

 

プリプロセス後は次のように展開される。

CPutLog( __FILE__, __LINE__ )( fp, _T( "%s" ), _T( "これはテストです" ) );
CPutLog( __FILE__, __LINE__ )( fp, _T( "%d + %d = %d" ), 1, 3, 4 );

 

CPutLog( __FILE__, __LINE__ ) で CPutLog 型の一時オブジェクトが作成され、
その一時オブジェクトに対し、operator() 呼び出しがされる。

 

■出力例

[c:\xxxx\test.cpp](296): これはテストです
[c:\xxxx\test.cpp](297): 1 + 34

投稿日時 : 2006年12月10日 5:36

コメントを追加

# re: マクロで可変長引数を使う方法 2006/12/10 19:17 crimsonwoods

C99からは可変長引数を取る関数型マクロを
書くことが出来ます。
VS2005では対応済みで、

#include <stdio.h>
#include <tchar.h>

#define VAARG_FUNC( format, ... ) printf( format, __VA_ARGS__ );

int _tmain( int argc, _TCHAR** argv )
{
VAARG_FUNC( "%s %d %f\n", "hoge", 10, 0.31 );

return 0;
}

みたいなことが出来るようになっています。

# re: マクロで可変長引数を使う方法 2006/12/10 23:41 RAPT

おっと、そういえば、C99 は可変長引数を取るマクロが追加されていましたっけ。
結構昔に聞いたことがありますが、使っていないので忘れていました。(未だにVC++6がメイン)

# I delight in, result in I discovered exactly what I used to be having a look for. You've ended my four day long hunt! God Bless you man. Have a great day. Bye 2018/10/06 11:29 I delight in, result in I discovered exactly what

I delight in, result in I discovered exactly what I used to be having a look
for. You've ended my four day long hunt!
God Bless you man. Have a great day. Bye

# What's Happening i'm new to this, I stumbled upon this I have found It absolutely useful and it has aided me out loads. I am hoping to contribute & help other users like its helped me. Good job. 2018/10/08 5:50 What's Happening i'm new to this, I stumbled upon

What's Happening i'm new to this, I stumbled upon this I have found It absolutely useful and it has aided me
out loads. I am hoping to contribute & help other users like its helped me.
Good job.

# This post will assist the internet viewers for setting up new webpage or even a blog from start to end. 2018/10/11 23:32 This post will assist the internet viewers for set

This post will assist the internet viewers for setting up new webpage or even a blog from start to
end.

# I couldn't resist commenting. Exceptionally well written! 2019/04/04 4:10 I couldn't resist commenting. Exceptionally well w

I couldn't resist commenting. Exceptionally well written!

# Howdy I am so delighted I found your weblog, I really found you by mistake, while I was researching on Digg for something else, Nonetheless I am here now and would just like to say thanks a lot for a remarkable post and a all round enjoyable blog (I als 2019/04/17 3:41 Howdy I am so delighted I found your weblog, I rea

Howdy I am so delighted I found your weblog, I really found
you by mistake, while I was researching on Digg for something else, Nonetheless
I am here now and would just like to say thanks a lot for a remarkable post and a all round
enjoyable blog (I also love the theme/design), I don't have time
to go through it all at the minute but I have bookmarked it
and also included your RSS feeds, so when I have time I will be back to read more,
Please do keep up the superb work.

# Hi! This is my 1st comment here so I just wanted to give a quick shout out and tell you I truly enjoy reading through your articles. Can you recommend any other blogs/websites/forums that go over the same topics? Thanks a ton! 2019/05/09 9:15 Hi! This is my 1st comment here so I just wanted t

Hi! This is my 1st comment here so I just wanted to give a quick shout out
and tell you I truly enjoy reading through your
articles. Can you recommend any other blogs/websites/forums that go over the same topics?

Thanks a ton!

# When some one searches for his necessary thing, so he/she wishes to be available that in detail, so that thing is maintained over here. 2019/05/09 15:38 When some one searches for his necessary thing, so

When some one searches for his necessary thing, so he/she wishes to
be available that in detail, so that thing is maintained
over here.

# It's a pity you don't have a donate button! I'd definitely donate to this excellent blog! I guess for now i'll settle for book-marking and adding your RSS feed to my Google account. I look forward to brand new updates and will share this website with m 2019/05/31 6:09 It's a pity you don't have a donate button! I'd de

It's a pity you don't have a donate button! I'd definitely donate to this excellent blog!

I guess for now i'll settle for book-marking and adding your RSS feed to my Google account.
I look forward to brand new updates and will share this website
with my Facebook group. Chat soon!

# Wonderful blog! I found it while browsing on Yahoo News. Do you have any suggestions on how to get listed in Yahoo News? I've been trying for a while but I never seem to get there! Thanks 2019/06/01 9:56 Wonderful blog! I found it while browsing on Yahoo

Wonderful blog! I found it while browsing on Yahoo News.
Do you have any suggestions on how to get listed in Yahoo News?
I've been trying for a while but I never seem
to get there! Thanks

# This is my first time pay a visit at here and i am actually happy to read all at single place. 2019/06/04 19:56 This is my first time pay a visit at here and i am

This is my first time pay a visit at here and i am actually happy to read
all at single place.

# hi!,I like your writing very so much! proportion we keep in touch more approximately your post on AOL? I need an expert in this area to resolve my problem. May be that is you! Looking ahead to look you. 2019/06/12 19:39 hi!,I like your writing very so much! proportion w

hi!,I like your writing very so much! proportion we keep
in touch more approximately your post on AOL? I need an expert in this area to
resolve my problem. May be that is you! Looking ahead to look you.

# Hi there just wanted to give you a quick heads up. The words in your content seem to be running off the screen in Internet explorer. I'm not sure if this is a formatting issue or something to do with browser compatibility but I thought I'd post to let yo 2019/08/20 5:47 Hi there just wanted to give you a quick heads up.

Hi there just wanted to give you a quick heads up. The words in your content seem to be running off the screen in Internet
explorer. I'm not sure if this is a formatting issue or something to do with browser compatibility but I thought I'd post to let you know.
The layout look great though! Hope you get the issue resolved soon. Thanks

# Hi there just wanted to give you a quick heads up. The words in your content seem to be running off the screen in Internet explorer. I'm not sure if this is a formatting issue or something to do with browser compatibility but I thought I'd post to let yo 2019/08/20 5:48 Hi there just wanted to give you a quick heads up.

Hi there just wanted to give you a quick heads up. The words in your content seem to be running off the screen in Internet
explorer. I'm not sure if this is a formatting issue or something to do with browser compatibility but I thought I'd post to let you know.
The layout look great though! Hope you get the issue resolved soon. Thanks

# Hi there just wanted to give you a quick heads up. The words in your content seem to be running off the screen in Internet explorer. I'm not sure if this is a formatting issue or something to do with browser compatibility but I thought I'd post to let yo 2019/08/20 5:49 Hi there just wanted to give you a quick heads up.

Hi there just wanted to give you a quick heads up. The words in your content seem to be running off the screen in Internet
explorer. I'm not sure if this is a formatting issue or something to do with browser compatibility but I thought I'd post to let you know.
The layout look great though! Hope you get the issue resolved soon. Thanks

# Hi there just wanted to give you a quick heads up. The words in your content seem to be running off the screen in Internet explorer. I'm not sure if this is a formatting issue or something to do with browser compatibility but I thought I'd post to let yo 2019/08/20 5:50 Hi there just wanted to give you a quick heads up.

Hi there just wanted to give you a quick heads up. The words in your content seem to be running off the screen in Internet
explorer. I'm not sure if this is a formatting issue or something to do with browser compatibility but I thought I'd post to let you know.
The layout look great though! Hope you get the issue resolved soon. Thanks

# jvyjakylwhdv 2021/11/28 14:49 cegoppoa

https://chloroquinecan.com/ chloroquine cvs

# zcvshsxtdyxt 2021/12/01 19:34 dwedaywspc

how to get hydroxychloroquine https://hydroxywithchloroquine.com/

# Hi! I simply want to offer you a huge thumbs up for the excellent information you have got here on this post. I will be coming back to your web site for more soon. 2022/02/17 19:17 Hi! I simply want to offer you a huge thumbs up fo

Hi! I simply want to offer you a huge thumbs up for
the excellent information you have got here on this post.
I will be coming back to your web site for more soon.

# I as well as my guys have been following the best procedures found on the blog and so all of the sudden got an awful suspicion I had not expressed respect to the blog owner for those secrets. The young boys came totally glad to study all of them and hav 2022/02/18 16:11 I as well as my guys have been following the best

I as well as my guys have been following the best procedures found on the blog and
so all of the sudden got an awful suspicion I had not expressed respect to the blog owner for those secrets.
The young boys came totally glad to study all of them and
have in effect actually been taking pleasure
in those things. Appreciate your actually being
indeed considerate and for obtaining this kind of fine tips millions of individuals are really desperate to understand about.

Our own sincere apologies for not expressing appreciation to you sooner.

# I pay a visit daily a few web pages and websites to read articles, except this web site presents feature based content. 2022/02/19 9:34 I pay a visit daily a few web pages and websites

I pay a visit daily a few web pages and websites to read articles, except this web site presents feature based content.

# I was suggested this website by my cousin. I am not sure whether this post is written by him as nobody else know such detailed about my problem. You are amazing! Thanks! 2022/02/19 21:38 I was suggested this website by my cousin. I am no

I was suggested this website by my cousin. I am not sure whether
this post is written by him as nobody else know such
detailed about my problem. You are amazing! Thanks!

# It's really very complex in this busy life to listen news on TV, therefore I just use world wide web for that purpose, and get the most recent information. 2022/02/20 12:20 It's really very complex in this busy life to list

It's really very complex in this busy life to listen news on TV, therefore I just use world wide web for that purpose, and get the
most recent information.

# Its like you read my mind! You seem to know a lot about this, like you wrote the book in it or something. I think that you could do with some pics to drive the message home a bit, but instead of that, this is excellent blog. A great read. I'll certainly 2022/02/20 12:45 Its like you read my mind! You seem to know a lot

Its like you read my mind! You seem to know a lot about this, like you wrote the book in it or
something. I think that you could do with some pics to drive the message home a bit,
but instead of that, this is excellent blog.
A great read. I'll certainly be back.

# First off I would like to say wonderful blog! I had a quick question that I'd like to ask if you do not mind. I was interested to know how you center yourself and clear your head before writing. I have had trouble clearing my mind in getting my thoughts 2022/02/21 0:05 First off I would like to say wonderful blog! I ha

First off I would like to say wonderful blog!

I had a quick question that I'd like to ask if you do not mind.
I was interested to know how you center yourself and clear your head before writing.

I have had trouble clearing my mind in getting my thoughts
out there. I do take pleasure in writing but it just seems like the first 10 to 15 minutes tend to be lost simply just trying to figure out how to begin. Any suggestions or hints?

Thanks!

# Great blog you've got here.. It?s hard to find high-quality writing like yours nowadays. I seriously appreciate people like you! Take care!! 2022/02/22 9:09 Great blog you've got here.. It?s hard to find hig

Great blog you've got here.. It?s hard to find high-quality writing like yours nowadays.
I seriously appreciate people like you! Take care!!

# Ahaa, its fastidious dialogue about this paragraph at this place at this web site, I have read all that, so now me also commenting at this place. 2022/02/22 9:13 Ahaa, its fastidious dialogue about this paragraph

Ahaa, its fastidious dialogue about this paragraph at this place at this web site,
I have read all that, so now me also commenting at this place.

# Really informative and good bodily structure of content material, now that's user genial (: . 2022/02/24 1:34 Really informative and good bodily structure of co

Really informative and good bodily structure of content material,
now that's user genial (:.

# You should take part in a contest for one of the highest quality blogs on the web. I'm going to highly recommend this site! 2022/03/04 7:06 You should take part in a contest for one of the h

You should take part in a contest for one of the highest quality blogs on the web.
I'm going to highly recommend this site!

# I went over this website and I conceive you have a lot of fantastic info, bookmarked (:. 2022/03/05 1:45 I went over this website and I conceive you have a

I went over this website and I conceive you have a lot of
fantastic info, bookmarked (:.

# Some truly marvellous work on behalf of the owner of this internet site, utterly great content. 2022/03/05 8:40 Some truly marvellous work on behalf of the owner

Some truly marvellous work on behalf of the owner of this internet site, utterly great content.

# Outstanding post, I believe people should acquire a lot from this web site its rattling user genial. So much good info on here :D. 2022/03/05 20:03 Outstanding post, I believe people should acquire

Outstanding post, I believe people should acquire a lot from this web site its
rattling user genial. So much good info on here
:D.

# Thanks for every other fantastic article. Where else could anybody get that kind of information in such an ideal approach of writing? I've a presentation next week, and I am on the look for such information. 2022/03/07 8:27 Thanks for every other fantastic article. Where e

Thanks for every other fantastic article. Where else could anybody get that kind of information in such an ideal approach of writing?
I've a presentation next week, and I am on the look for such information.

# I always was interested in this topic and still am, regards for posting. 2022/03/07 8:33 I always was interested in this topic and still am

I always was interested in this topic and still am, regards for posting.

# It's in fact very complicated in this busy life to listen news on TV, thus I only use web for that purpose, and get the newest news. 2022/03/08 15:54 It's in fact very complicated in this busy life to

It's in fact very complicated in this busy life to listen news on TV, thus I only use web for that purpose,
and get the newest news.

# I all the time used to read article in news papers but now as I am a user of web thus from now I am using net for articles, thanks to web. 2022/03/09 2:41 I all the time used to read article in news papers

I all the time used to read article in news papers but
now as I am a user of web thus from now I am using net for articles, thanks to web.

# You need to be a part of a contest for one of the finest sites on the net. I will highly recommend this site! 2022/03/10 18:29 You need to be a part of a contest for one of the

You need to be a part of a contest for one of the finest sites on the
net. I will highly recommend this site!

# Real good visual appeal on this site, I'd value it 10. 2022/03/11 5:00 Real good visual appeal on this site, I'd value it

Real good visual appeal on this site, I'd value it 10.

# Hi! I know this is somewhat off topic but I was wondering which blog platform are you using for this website? I'm getting fed up of Wordpress because I've had issues with hackers and I'm looking at alternatives for another platform. I would be great if 2022/03/24 7:38 Hi! I know this is somewhat off topic but I was wo

Hi! I know this is somewhat off topic but I was wondering which blog platform are you using for this website?
I'm getting fed up of Wordpress because
I've had issues with hackers and I'm looking at alternatives for another platform.
I would be great if you could point me in the direction of a good platform.

# Hi! I know this is somewhat off topic but I was wondering which blog platform are you using for this website? I'm getting fed up of Wordpress because I've had issues with hackers and I'm looking at alternatives for another platform. I would be great if 2022/03/24 7:39 Hi! I know this is somewhat off topic but I was wo

Hi! I know this is somewhat off topic but I was wondering which blog platform are you using for this website?
I'm getting fed up of Wordpress because
I've had issues with hackers and I'm looking at alternatives for another platform.
I would be great if you could point me in the direction of a good platform.

# Hi! I know this is somewhat off topic but I was wondering which blog platform are you using for this website? I'm getting fed up of Wordpress because I've had issues with hackers and I'm looking at alternatives for another platform. I would be great if 2022/03/24 7:40 Hi! I know this is somewhat off topic but I was wo

Hi! I know this is somewhat off topic but I was wondering which blog platform are you using for this website?
I'm getting fed up of Wordpress because
I've had issues with hackers and I'm looking at alternatives for another platform.
I would be great if you could point me in the direction of a good platform.

# Hi! I know this is somewhat off topic but I was wondering which blog platform are you using for this website? I'm getting fed up of Wordpress because I've had issues with hackers and I'm looking at alternatives for another platform. I would be great if 2022/03/24 7:41 Hi! I know this is somewhat off topic but I was wo

Hi! I know this is somewhat off topic but I was wondering which blog platform are you using for this website?
I'm getting fed up of Wordpress because
I've had issues with hackers and I'm looking at alternatives for another platform.
I would be great if you could point me in the direction of a good platform.

# vcyrvegsuams 2022/05/20 17:45 fpexaozo

buy erythromycin online https://erythromycin1m.com/#

# college essay ideas help i612op 2022/09/03 17:30 Charlosmox


Fantastic info, Cheers! https://definitionessays.com/ academic essay

# hi!,I like your writing so much! proportion we be in contact more about your post on AOL? I require an expert on this house to unravel my problem. May be that's you! Taking a look forward to peer you. 2023/01/12 9:01 hi!,I like your writing so much! proportion we be

hi!,I like your writing so much! proportion we
be in contact more about your post on AOL? I require an expert on this house to unravel
my problem. May be that's you! Taking a look forward to peer you.

# term paper writer service v15mth 2023/02/10 23:42 Albertosed

You explained it well.
https://essaywritingservicelinked.com/ writing the personal essay

# paying someone to write your paper h21bie 2023/02/26 10:22 CharlesSnoff


Nicely put, Appreciate it.
pay for essay https://quality-essays.com/ buy admission essay

# how to write a graduate admissions essay a47nzw 2023/03/06 6:34 Gregorysaipt

You reported that really well.
writing a short essay https://englishessayhelp.com college essay admission https://dissertationwritingtops.com

# business dissertation topics r87uiv 2023/03/06 18:50 EugeneSib


Awesome info. Appreciate it.
write essays for me https://topswritingservices.com dissertation questionnaire https://essaytyperhelp.com

# writing a descriptive essay d32dff 2023/03/07 9:44 EugeneSib


Info very well applied!.
write my dissertation https://domyhomeworkformecheap.com how to write five paragraph essay https://custompaperwritingservices.com

# college essay competitions e602sa 2023/03/08 1:40 EugeneSib


Truly lots of very good advice!
how to buy an essay online https://custompaperwritingservices.com dissertation help uk https://argumentativethesis.com

# find phd thesis h92eny 2023/03/08 3:39 Gregorysaipt


You definitely made your point.
help writing essay https://dissertationwritingtops.com help me write a descriptive essay https://ouressays.com

# cheap ghost writer services b536gv 2023/03/09 1:55 Gregorysaipt

You actually expressed it perfectly.
essay writing prompts for high school https://researchproposalforphd.com letter writing services https://essaypromaster.com

# phd thesis introduction c40iqm 2023/03/09 9:38 EugeneSib


Terrific stuff. Cheers.
what is a good essay writing service https://researchproposalforphd.com who can i pay to write my essay https://essaywritingservicetop.com

# write my personal statement z32wqw 2023/03/09 23:23 Gregorysaipt


Thanks! Quite a lot of knowledge.
help writing essays for college https://custompaperwritersservices.com type of essay writing https://buycheapessaysonline.com

# i need help with my essay r684vf 2023/03/10 20:52 Gregorysaipt

You actually expressed this well!
dissertation scholarships https://helptowriteanessay.com how to write a argumentative essay https://domyhomeworkformecheap.com

# freedom writer essay v96voq 2023/03/11 5:48 EugeneSib


You said it perfectly.!
master thesis writing help https://domyhomeworkformecheap.com how to write a interview essay https://helpwritingdissertation.com

# thesis versus dissertation a215fm 2023/03/12 11:21 EugeneSib


Regards! An abundance of write ups.
how to write a descriptive essay about a place https://helpwithdissertationwriting.com cheapest article writing service https://dissertationwritingtops.com

# cheap assignment writing service w46bym 2023/03/13 14:06 Gregorysaipt


Thanks a lot! Wonderful information.
how to write an interview essay https://cheapessaywriteronlineservices.com custom writers https://bestmasterthesiswritingservice.com

# nursing essay writing y98yfa 2023/03/13 16:32 EugeneSib

You actually reported it well!
top essay writers https://writingresearchtermpaperservice.com steps in writing an essay https://essaywritingservicebbc.com

# doors2.txt;1 2023/03/14 16:21 ffWfZKzbLVAom

doors2.txt;1

タイトル
名前
URL
コメント