Topic: Type declaration and parameter declaration


Author: v.Abazarov@comAcast.net (Victor Bazarov)
Date: Fri, 8 Jul 2005 03:31:54 GMT
Raw View
Maciej Sobczak wrote:
> Consider the following (C++):
>
> void fun(int);
>
> I have a terminology question considering the above declaration and its
> parts.
>
> 1. Is the "int" above declaring a type?

No.  A type declaration starts with 'class' or 'struct'.

> 2. Is the "int" above declaring a first parameter?

No.

 > Note that there is no
> name of the parameter provided - is it possible to declare anything
> without providing its name?

Of course not.

> 3. Is the declaration-of-the-parameter-type part of the
> declaration-of-the-function-parameter?

Yes.

> When asked, I claimed "no" to all of the above.

Too bad.

 > I'm asking here to get
> authoritative confirmation or negation of this claim.

In the statement above, 'fun' is a declaration of a function because there
is a pair of parentheses after "void foo", which contains a list of
declarations (consisting of a single declaration, which itself consists of
a simple type-id).  Just read 8.3.5 _carefully_.

V

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: jackklein@spamcop.net (Jack Klein)
Date: Fri, 8 Jul 2005 03:42:25 GMT
Raw View
On Thu,  7 Jul 2005 13:32:39 CST, Maciej Sobczak <no.spam@no.spam.com>
wrote in comp.std.c++:

> Hello,
>=20
> Consider the following (C++):
>=20
> void fun(int);
>=20
> I have a terminology question considering the above declaration and its=
=20
> parts.
>=20
> 1. Is the "int" above declaring a type?

Yes.  It is declaring the type of the one and only argument that the
function accepts.

> 2. Is the "int" above declaring a first parameter? Note that there is n=
o=20
> name of the parameter provided - is it possible to declare anything=20
> without providing its name?

No.  Not a first parameter, the one and only parameter.  The name of
the parameter is specifically unnecessary in "prototype scope".  If a
name is included, its scope ends at the semicolon that terminates the
declaration.

This is perfectly legal:

void fun(int);
void fun(int a);
void fun(int z);
void fun(int val)
{
   std::cout << val << std::endl;
}

They are all declarations, and one a definition, of the same function.
The 'a', the 'z', and the 'val' do not conflict because they are in
different scopes.

> 3. Is the declaration-of-the-parameter-type part of the=20
> declaration-of-the-function-parameter?

Neither of these terms is defined by the standard, you need to define
exactly what you mean by them.

Does this answer your question:

"An identifier can optionally be provided as a parameter name; if
present in a function definition (8.4), it names a parameter
(sometimes called =93formal argument=94). [Note: in particular, parameter
names are also optional in function definitions and names used for a
parameter in different declarations and the definition of a function
need not be the same. If a parameter name is present in a function
declaration that is not a definition, it cannot be used outside of the
parameter-declaration-clause since it goes out of scope at the
end of the function declarator (3.3). ]"

> When asked, I claimed "no" to all of the above. I'm asking here to get=20
> authoritative confirmation or negation of this claim.
>=20
> Regards,

As to where to get definitive answers, I would suggest section 8.3.5
of the standard, which covers function declarations.  The quotation
above is paragraph 8 of that section.

--=20
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~ajo/docs/FAQ-acllc.html

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: kuyper@wizard.net
Date: Thu, 7 Jul 2005 23:35:57 CST
Raw View
Maciej Sobczak wrote:
> Hello,
>
> Consider the following (C++):
>
> void fun(int);
>
> I have a terminology question considering the above declaration and its
> parts.
>
> 1. Is the "int" above declaring a type?

It declares that the function has a parameter of type 'int'.

> 2. Is the "int" above declaring a first parameter? Note that there is no
> name of the parameter provided - is it possible to declare anything
> without providing its name?

When you're providing a function declaration that is not also a
definition, the parameter names are pointless, and therefore they've
been made optional.

> 3. Is the declaration-of-the-parameter-type part of the
> declaration-of-the-function-parameter?

Yes.

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Maciej Sobczak <no.spam@no.spam.com>
Date: Sat, 9 Jul 2005 09:20:52 CST
Raw View
Hi,

Victor Bazarov wrote:

>> void fun(int);
>>
>> 1. Is the "int" above declaring a type?
>
> No.  A type declaration starts with 'class' or 'struct'.

>> 3. Is the declaration-of-the-parameter-type part of the
>> declaration-of-the-function-parameter?
>
> Yes.

Why? Especially in the light of your first answer?
Actually, all questions were related. If we agree that we don't declare
types in function declarations (1st question), then there cannot be such
thing like a declaration of the parameter type within declaration of the
parameter. Of course, the declaration of the function parameter needs
the type specifier, but the type itself is not declared there. No new
name of any type is introduced when declaring function parameter.
That's why I claim "no" to the third question as well.

We might elaborate the whole thing:

class T; // class declaration, introduces name "T"

void fun(T *p);

Above, "T*" is a name of the type of the first and only parameter of the
function fun. Is this declaring the type "pointer to T"? I'd say no,
even taking into account that this is a composite type.

Regards,

--
Maciej Sobczak : http://www.msobczak.com/
Programming    : http://www.msobczak.com/prog/

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Marc Schoolderman <squell@alumina.nl>
Date: 9 Jul 2005 18:40:02 GMT
Raw View
Maciej Sobczak wrote:

Considering the mixed reactions so far :) I thought I'd look this up.

> void fun(int);

> 1. Is the "int" above declaring a type?

No. It declares a parameter. A parameter declaration is not allowed to
_define_ types (8.3.5/6). However; you can declare class types without
defining them, so in

    void fun(struct foo);

"struct foo" is a type declaration as well as a parameter declaration.
The name 'foo' is also introduced outside the fuction (3.3.1/5).

> 2. Is the "int" above declaring a first parameter? Note that there is no
> name of the parameter provided - is it possible to declare anything
> without providing its name?

Yes to the first question:

8.3.5/8: "An identifier can optionally be provided as a parameter name".

No to the second (*):

3.1/1: "A declaration introduces names [..] or redeclares names
introduced by previous declarations"

The subtle point is, a parameter declaration is not a declaration, but
just a part of a function declarator, which of course introduces a name
- that of the function.



(*): This is probably too much information, but you can create unnamed
bitfields using *the syntax* of a declaration that does not introduce
any names (7/3, 9.6/2):

struct foo {
     int : 1;
};

~Marc

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: v.Abazarov@comAcast.net ("Victor Bazarov")
Date: Mon, 11 Jul 2005 16:44:17 GMT
Raw View
Maciej Sobczak wrote:
> Victor Bazarov wrote:
>
>>> void fun(int);
>>>
>>> 1. Is the "int" above declaring a type?
>>
>> No.  A type declaration starts with 'class' or 'struct'.
>
>>> 3. Is the declaration-of-the-parameter-type part of the
>>> declaration-of-the-function-parameter?
>>
>> Yes.
>
> Why? Especially in the light of your first answer?

I don't understand the question.  If you had

   void fun(class T);

then what goes inside the parentheses is a _type_declaration_.  If
you begin it with a type-id (like "int"), it's not a declaration of
a type.  It's a declarator for the function argument.

> Actually, all questions were related. If we agree that we don't
> declare types in function declarations (1st question),

But we can if we want to.  So, no, we don't agree.

> then there
> cannot be such thing like a declaration of the parameter type within
> declaration of the parameter. Of course, the declaration of the
> function parameter needs the type specifier, but the type itself is
> not declared there. No new name of any type is introduced when
> declaring function parameter. That's why I claim "no" to the third
> question as well.
> We might elaborate the whole thing:
>
> class T; // class declaration, introduces name "T"
>
> void fun(T *p);
>
> Above, "T*" is a name of the type of the first and only parameter of
> the function fun. Is this declaring the type "pointer to T"? I'd say
> no, even taking into account that this is a composite type.

T* is not a name of the type.  T is the name of the type.  T* is
a declarator.  If you had

   class T;
   typedef T* pT;
   void fun(pT);

then 'pT' is a typedef-id, which is allowed to be used here.  It is
still a declarator.

V


---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]





Author: Maciej Sobczak <no.spam@no.spam.com>
Date: Thu, 7 Jul 2005 13:32:39 CST
Raw View
Hello,

Consider the following (C++):

void fun(int);

I have a terminology question considering the above declaration and its
parts.

1. Is the "int" above declaring a type?

2. Is the "int" above declaring a first parameter? Note that there is no
name of the parameter provided - is it possible to declare anything
without providing its name?

3. Is the declaration-of-the-parameter-type part of the
declaration-of-the-function-parameter?

When asked, I claimed "no" to all of the above. I'm asking here to get
authoritative confirmation or negation of this claim.

Regards,

--
Maciej Sobczak : http://www.msobczak.com/
Programming    : http://www.msobczak.com/prog/

---
[ 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.jamesd.demon.co.uk/csc/faq.html                       ]