C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Parenthesis around types?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards
View previous topic :: View next topic  
Author Message
Guest






PostPosted: Mon May 14, 2007 5:43 pm    Post subject: Parenthesis around types? Reply with quote



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 ]
Back to top
Alberto Ganesh Barbati
Guest





PostPosted: Tue May 15, 2007 2:24 am    Post subject: Re: Parenthesis around types? Reply with quote



jpalecek (AT) web (DOT) de ha scritto:
Quote:
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 ]
Back to top
Roman.Perepelitsa@gmail.c
Guest





PostPosted: Tue May 15, 2007 7:24 pm    Post subject: Re: Parenthesis around types? Reply with quote



Quote:
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 ]
Back to top
Guest






PostPosted: Tue May 15, 2007 7:24 pm    Post subject: Re: Parenthesis around types? Reply with quote

On May 14, 11:24 pm, AlbertoBarb...@libero.it (Alberto Ganesh Barbati)
wrote:
Quote:
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 ]
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language, library and standards All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.