From: Gerasimos Karvounis on
Hello,
today I was searching for over an hour for the reason of a compiler
error after a change I made (finally I found it). That was really a
strange error! I do not know, if the "original" code was legal C++.

Here an example:

class A {
public:
A(), // Notice the comma instead of a semicolon!
A(int i);

private:
int mValue;
};


class __declspec(dllexport) B {
public:
B();
B(int i);

private:
int mValue;
};


A::A()
: mValue(0)
{
std::cout << "A's default constructor called!" << std::endl;
}
A::A(int i)
: mValue(i)
{
std::cout << "A's int-constructor called!" << std::endl;
}

B::B()
: mValue(0)
{
std::cout << "B's default constructor called!" << std::endl;
}
B::B(int i)
: mValue(i)
{
std::cout << "B's int-constructor called!" << std::endl;
}


int main(int argc, char *argv[])
{
A a1;
A a2(2);

B b1;
B b2(2);

return 0;
}

This compiles fine under VC++2005 although A's default constructor
declaration is terminated with a comma (is the comma operator allowed
here???).

But, if I change the declaration of B into:

class __declspec(dllexport) B {
public:
B(), // Notice the comma instead of a semicolon!
B(int i);

private:
int mValue;
};

Then my compiler reports error C2487: member of dll interface class may
not be declared with dll interface

So my questions are:
1. Is "terminating" comma in a class constructor declaration legal?
2. Is it a compiler bug?


Thanks!

Makis

--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Johannes Schaub (litb) on
Gerasimos Karvounis wrote:

> Hello,
> today I was searching for over an hour for the reason of a compiler
> error after a change I made (finally I found it). That was really a
> strange error! I do not know, if the "original" code was legal C++.
>
> Here an example:
>
> class A {
> public:
> A(), // Notice the comma instead of a semicolon!
> A(int i);
>
> private:
> int mValue;
> };
>
>
> ...
>
> So my questions are:
> 1. Is "terminating" comma in a class constructor declaration legal?
>
Yes, they are legal. 7/7 in the Standard:

Only in function declarations for constructors, destructors, and type
conversions can the decl-specifier-seq be omitted.

From this it follows that the "A" parts in the declaration syntactically
match the declarator-id in the declarator-portions, not the type-specifier
in the decl-specifier-seq portion which appears only once at the front of a
declaration. You can do

struct X { int a(), b(); };

And equally well you can do

struct X { X(), X(int); };


--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Daniel Krügler on
On 16 Okt., 02:15, Gerasimos Karvounis <Gerasimos.Karvou...(a)gmx.de>
wrote:
> today I was searching for over an hour for the reason of a compiler
> error after a change I made (finally I found it). That was really a
> strange error! I do not know, if the "original" code was legal C++.
>
> Here an example:
>
> class A {
> public:
> A(), // Notice the comma instead of a semicolon!
> A(int i);
> private:
> int mValue;
> };
>
> class __declspec(dllexport) B {
> public:
> B();
> B(int i);
>
> private:
> int mValue;
>
> };
>
> A::A()
> : mValue(0)
> {
> std::cout << "A's default constructor called!" << std::endl;}
>
> A::A(int i)
> : mValue(i)
> {
> std::cout << "A's int-constructor called!" << std::endl;
>
> }
>
> B::B()
> : mValue(0)
> {
> std::cout << "B's default constructor called!" << std::endl;}
>
> B::B(int i)
> : mValue(i)
> {
> std::cout << "B's int-constructor called!" << std::endl;
>
> }
>
> int main(int argc, char *argv[])
> {
> A a1;
> A a2(2);
>
> B b1;
> B b2(2);
>
> return 0;
>
> }
>
> This compiles fine under VC++2005 although A's default constructor
> declaration is terminated with a comma (is the comma operator allowed
> here???).

This is no comma operator, it is a comma that is part of a sequence
of declarations. Except for a situation where name hiding occurs the
sequence

T D1, D2, ... Dn;

is equivalent to

T D1; T D2; ... T Dn;

see [dcl.decl].

> But, if I change the declaration of B into:
>
> class __declspec(dllexport) B {
> public:
> B(), // Notice the comma instead of a semicolon!
> B(int i);
> private:
> int mValue;
> };
>
> Then my compiler reports error C2487: member of dll interface class may
> not be declared with dll interface
>
> So my questions are:
> 1. Is "terminating" comma in a class constructor declaration legal?

Yes, if it is part of a declaration sequence.

> 2. Is it a compiler bug?

It's hard to say, because __declspec is a non-standard
component. In which way it interferes with the declaration
sequence is out of the scope of the standard.

HTH & Greetings from Bremen,

Daniel Kr�gler




--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

From: Greg Herlihy on
On Oct 16, 11:10 am, Daniel Kr�gler <daniel.krueg...(a)googlemail.com>
wrote:
> On 16 Okt., 02:15, Gerasimos Karvounis <Gerasimos.Karvou...(a)gmx.de>
> wrote:
> > But, if I change the declaration of B into:
>
> > class __declspec(dllexport) B {
> > public:
> > B(), // Notice the comma instead of a semicolon!
> > B(int i);
> > private:
> > int mValue;
> > };
>
> > Then my compiler reports error C2487: member of dll interface class may
> > not be declared with dll interface
> ...
> > 2. Is it a compiler bug?
>
> It's hard to say, because __declspec is a non-standard
> component. In which way it interferes with the declaration
> sequence is out of the scope of the standard.

The "__declspec" identifier is not "non-standard" C++. To call
__declspec "non-standard" means that its presence in a program
violates - or at least - disregards the C++ Standard. The clear
implication is that a compiler that recognizes this "non-standard"
identifier - cannot be a "true" (or "valid" or "conforming") C++
compiler.

In reality, "__declspec" is an "implementation-defined" identifier,
and one that fully conforms to the C++ Standard: "Each name that
contains a double underscore (__) ... is reserved to the
implementation for any use."[�17.4.3.1.2].

Furthermore, there is no basis for concluding that __declspec somehow
"interferes" with the Standard C++ meaning of the declaration. And in
fact, it does not.

Greg



--
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

 | 
Pages: 1
Prev: Shallow\Deep copy
Next: Simplest MetaLoop