宏变量args [英] Macro var args
问题描述
大家好,
现在,我正在使用以下宏自动添加信息
异常:
#define THROW(Class,args ...)抛出类(__ FILE __,_ _ _ 3,## args)
但AFAIK,它是特定于gcc的。有没有办法以标准的c ++方式进行?
TIA
-
TheDD
TheDD< pa ********* @ pas.de.spam.fr>写在
新闻:c8 ********** @ news-reader2.wanadoo.fr:
大家好,>
现在,我正在使用以下宏自动添加异常信息:
#define THROW(Class,args ...)throw Class(__ FILE__ ,__LINE __,## args)
但是AFAIK,它是特定的gcc。有没有办法以标准的c ++方式做到这一点?
不是真的。
虽然,这是一个非常有趣的替代技术。
请参阅Andrei Alexandrescu和John Torjo的以下文章。
http://www.cuj.com/documents/s=8464/...8alexandr.html
-
:: bartekd [at] o2 [dot] pl
< blockquote> TheDD写道:
大家好,
现在,我正在使用以下宏自动添加信息
异常:
#define THROW(Class,args ...)抛出类(__ FILE __,_ _ _ 3,## args)
但是AFAIK,它是特定于gcc的。有没有办法以标准的c ++方式实现?
TIA
语法如下:
投掷< Class>(...)
这是一个例子:
#define投掷投掷者(__ FILE __,__ ____)。 DoThrow
struct Thrower
{
const char * file;
int line;
Thrower(const char * file,int line)
:文件(文件),行(行)
{
}
模板< typename T>
内联T DoThrow()
{
返回T(文件,行);
}
模板<
typename T,
typename T0
inline T DoThrow(T0& a0)
{
返回T(文件,行,a0);
}
模板<
typename T,
typename T0,
typename T1
inline T DoThrow(T0& a0,T1& a1)
{
返回T(文件,行) ,a0,a1);
}
模板<
typename T,
typename T0,
typename T1,
typename T2
inline T DoThrow(T0& a0,T1& a1,T2& a2)
{
返回T(文件,行,a0,a1,a2);
}
//为你需要的args定义DoThrow模板
};
struct Ex1
{
Ex1(const char * s);
};
struct Ex2
{
Ex2(const char * s,const char * s1);
};
void foo()
{
Throw< Ex2>(" A"," B");
抛出< Ex1>(A);
}
" TheDD" < PA ********* @ pas.de.spam.fr>写了...现在,我正在使用以下宏自动添加信息
异常:
#define THROW(Class,args。 ..)抛出类(__ FILE __,_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
但AFAIK,它是特定的gcc。有没有办法用标准的c ++
的方式呢?
编号通常的方法是拥有和你一样多的宏可能需要,
每个都有一个参数而不是最后一个。
#define THROW0(Class)throw Class(__ FILE __,__ LINE __)
#define THROW1(Class,a)抛出类(__ FILE __,__ LINE __,a)
等......
Victor
Hello all,
right now, i''m using the following macro to automatically add informations
to exceptions:
#define THROW(Class, args...) throw Class(__FILE__, __LINE__, ## args)
but AFAIK, it''s gcc specific. Is there a way to do it in a standard c++ way?
TIA
--
TheDD
TheDD <pa*********@pas.de.spam.fr> wrote in
news:c8**********@news-reader2.wanadoo.fr:
Hello all,
right now, i''m using the following macro to automatically add
informations to exceptions:
#define THROW(Class, args...) throw Class(__FILE__, __LINE__, ## args)
but AFAIK, it''s gcc specific. Is there a way to do it in a standard
c++ way?
Not really.
Though, there''s a very interesting alternative technique.
See the following article by Andrei Alexandrescu and John Torjo.
http://www.cuj.com/documents/s=8464/...8alexandr.html
--
:: bartekd [at] o2 [dot] pl
TheDD wrote:Hello all,
right now, i''m using the following macro to automatically add informations
to exceptions:
#define THROW(Class, args...) throw Class(__FILE__, __LINE__, ## args)
but AFAIK, it''s gcc specific. Is there a way to do it in a standard c++ way?
TIA
How about syntax like:
Throw<Class>( ... )
Here is an example:
#define Throw throw Thrower(__FILE__, __LINE__).DoThrow
struct Thrower
{
const char * file;
int line;
Thrower( const char * file, int line )
: file( file ), line( line )
{
}
template <typename T>
inline T DoThrow()
{
return T( file, line );
}
template <
typename T,
typename T0 inline T DoThrow( T0 & a0 )
{
return T( file, line, a0 );
}
template <
typename T,
typename T0,
typename T1 inline T DoThrow( T0 & a0, T1 & a1 )
{
return T( file, line, a0, a1 );
}
template <
typename T,
typename T0,
typename T1,
typename T2
inline T DoThrow( T0 & a0, T1 & a1, T2 & a2 )
{
return T( file, line, a0, a1, a2 );
}
// define the DoThrow template for as many args as you need
};
struct Ex1
{
Ex1( const char * s );
};
struct Ex2
{
Ex2( const char * s, const char * s1 );
};
void foo()
{
Throw<Ex2>( "A", "B ");
Throw<Ex1>( "A" );
}
"TheDD" <pa*********@pas.de.spam.fr> wrote...right now, i''m using the following macro to automatically add informations
to exceptions:
#define THROW(Class, args...) throw Class(__FILE__, __LINE__, ## args)
but AFAIK, it''s gcc specific. Is there a way to do it in a standard c++
way?
No. The usual approach is to have as many macros as you might ever need,
each with one more argument than the last one.
#define THROW0(Class) throw Class(__FILE__,__LINE__)
#define THROW1(Class,a) throw Class(__FILE__,__LINE__,a)
and so on...
Victor
这篇关于宏变量args的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!