Topic: Parenthesis around types?
Author: jpalecek@web.de
Date: Mon, 14 May 2007 11:43:32 CST Raw View
Hello,
I have a problem with C++. It seems that placing parenthesis around
types in declarators,
like this, is illegal
struct A;
void f((A)* a);
Is there any reason for that?
I would want it for this: I have a macro which takes a type and name
of a function and
makes a class with methods that call this function on the type.
However, I would like to
use a template tzpe as the parameter to the macro, which is a problem
because the
type name has comma in it (like this MACRO(TMPL<A,B>,something) which
can't
work. I can fix the macro problem by adding parentheses around the
type, like this
MACRO((TMPL<A,B>),something). But this will fail by creating a
parenthesised type
inside the macro.
Regards
Jiri Palecek
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: AlbertoBarbati@libero.it (Alberto Ganesh Barbati)
Date: Mon, 14 May 2007 21:24:12 GMT Raw View
jpalecek@web.de ha scritto:
> However, I would like to
> use a template tzpe as the parameter to the macro, which is a problem
> because the
> type name has comma in it (like this MACRO(TMPL<A,B>,something) which
> can't
> work. I can fix the macro problem by adding parentheses around the
> type, like this
> MACRO((TMPL<A,B>),something). But this will fail by creating a
> parenthesised type
> inside the macro.
You can also fix it like this:
MACRO(TMPL<A COMMA() B>, something)
where COMMA is defined with
#define COMMA() ,
See Boost.Preprocessor
<http://boost.org/libs/preprocessor/doc/index.html> for other
preprocessor techniques.
HTH,
Ganesh
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: jpalecek@web.de
Date: Tue, 15 May 2007 13:24:29 CST Raw View
On May 14, 11:24 pm, AlbertoBarb...@libero.it (Alberto Ganesh Barbati)
wrote:
> jpale...@web.de ha scritto:
>
> > However, I would like to
> > use a template tzpe as the parameter to the macro, which is a problem
> > because the
> > type name has comma in it (like this MACRO(TMPL<A,B>,something) which
> > can't
> > work. I can fix the macro problem by adding parentheses around the
> > type, like this
> > MACRO((TMPL<A,B>),something). But this will fail by creating a
> > parenthesised type
> > inside the macro.
>
> You can also fix it like this:
>
> MACRO(TMPL<A COMMA() B>, something)
>
> where COMMA is defined with
>
> #define COMMA() ,
>
Thank you.
Well, I have solved it another another way, I have defined macro
CLASS_NAME
to be TMPL<A,B> and it works.
However, I'm still curious about these parentheses. When they teach
you
C, in the lesson about complex type declarators, they will tell you
that
it's as if you added parentheses there and there. But this means that
you cannot add parentheses around types in the real life, I wonder, is
it
because of some dark corner of C++ (like function declaration/
variable
initialization disambiguation by extra parentheses)?
Jiri Palecek
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Roman.Perepelitsa@gmail.com" <Roman.Perepelitsa@gmail.com>
Date: Tue, 15 May 2007 13:24:20 CST Raw View
> I can fix the macro problem by adding parentheses around the
> type, like this
> MACRO((TMPL<A,B>),something). But this will fail by creating a
> parenthesised type
> inside the macro.
You can also force user to always put first argument to your macro in
parentheses.
template <class T>
struct Unwrap;
template <class P>
struct Unwrap<void (P)>
{
typedef P type;
};
#define GEN_FUN(Arg) void Foo(Unwrap<void Arg>::type) { }
// First argument to GEN_FUN should be put in parentheses
GEN_FUN((int))
GEN_FUN((vector<int>))
GEN_FUN((map<int, double>))
void Test(int i, vector<int> v, map<int, double> m)
{
Foo(i);
Foo(v);
Foo(m);
}
Roman Perepelitsa.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]