public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/20547] undefined reference to "static const" fields of classes
[not found] <bug-20547-10303@http.gcc.gnu.org/bugzilla/>
@ 2006-09-25 21:11 ` pinskia at gcc dot gnu dot org
0 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2006-09-25 21:11 UTC (permalink / raw)
To: gcc-bugs
------- Comment #11 from pinskia at gcc dot gnu dot org 2006-09-25 21:11 -------
*** Bug 29219 has been marked as a duplicate of this bug. ***
--
pinskia at gcc dot gnu dot org changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |etienne dot clement at
| |autodesk dot com
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20547
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/20547] undefined reference to "static const" fields of classes
2005-03-19 6:59 [Bug c++/20547] New: " Hu dot YuehWei at gmail dot com
` (8 preceding siblings ...)
2005-03-20 7:59 ` Hu dot YuehWei at gmail dot com
@ 2005-03-20 13:55 ` lerdsuwa at gcc dot gnu dot org
9 siblings, 0 replies; 11+ messages in thread
From: lerdsuwa at gcc dot gnu dot org @ 2005-03-20 13:55 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From lerdsuwa at gcc dot gnu dot org 2005-03-20 13:55 -------
Here is the relevant section of the standard (TC1,
section 9.4.2, paragraph 4):
If a 'static' data member is of 'const' integral or 'const' enumeral type,
its declaration in the class definition can specify a 'constant-initializer'
which shall be an integral constant expression (5.19). In that case, the
member can appear in integral constant expressions. The member shall still
be defined in a namespace scope if it is used in the program and the
namespace scope definition shall not contain an 'initializer'.
--
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution| |INVALID
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20547
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/20547] undefined reference to "static const" fields of classes
2005-03-19 6:59 [Bug c++/20547] New: " Hu dot YuehWei at gmail dot com
` (7 preceding siblings ...)
2005-03-19 18:36 ` pinskia at gcc dot gnu dot org
@ 2005-03-20 7:59 ` Hu dot YuehWei at gmail dot com
2005-03-20 13:55 ` lerdsuwa at gcc dot gnu dot org
9 siblings, 0 replies; 11+ messages in thread
From: Hu dot YuehWei at gmail dot com @ 2005-03-20 7:59 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From Hu dot YuehWei at gmail dot com 2005-03-20 07:59 -------
so the "... = 3;" initialization statement below is a definition or a declaration?
According to the C++ standard, its a definition.
Should the compiler allocate a memory space for it?
struct T
{
static char const a = 3;
};
And...
If I need to allocate memory spaces for it manually, why the standard explicitly
specify this kind of coding style?
========================
struct T
{
static char const a = 3; /* standard explicity specify that the initialization
& definition can be put here. */
};
char const T::a; /* If I need this line of codes, then the initialization above
should be a declaration, not a definition. But a declaration can be initialized? */
========================
struct T
{
static char const a;
};
char const T::a = 3;
========================
--
What |Removed |Added
----------------------------------------------------------------------------
Status|RESOLVED |UNCONFIRMED
Resolution|INVALID |
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20547
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/20547] undefined reference to "static const" fields of classes
2005-03-19 6:59 [Bug c++/20547] New: " Hu dot YuehWei at gmail dot com
` (6 preceding siblings ...)
2005-03-19 18:26 ` Hu dot YuehWei at gmail dot com
@ 2005-03-19 18:36 ` pinskia at gcc dot gnu dot org
2005-03-20 7:59 ` Hu dot YuehWei at gmail dot com
2005-03-20 13:55 ` lerdsuwa at gcc dot gnu dot org
9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-03-19 18:36 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From pinskia at gcc dot gnu dot org 2005-03-19 18:35 -------
Bacause T::A is a lvalue.
--
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution| |INVALID
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20547
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/20547] undefined reference to "static const" fields of classes
2005-03-19 6:59 [Bug c++/20547] New: " Hu dot YuehWei at gmail dot com
` (5 preceding siblings ...)
2005-03-19 14:34 ` pinskia at gcc dot gnu dot org
@ 2005-03-19 18:26 ` Hu dot YuehWei at gmail dot com
2005-03-19 18:36 ` pinskia at gcc dot gnu dot org
` (2 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: Hu dot YuehWei at gmail dot com @ 2005-03-19 18:26 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From Hu dot YuehWei at gmail dot com 2005-03-19 18:25 -------
I see.
But why compiler doesn't also make a temporary variable for the constant defined
in a class scope?
Such as following codes:
struct T
{
static int const a = 3;
};
void
fff(int const &a)
{
}
int
main()
{
fff(T::a); /* compiler will not make a temporary variable for it. */
fff(4); /* compiler will make a temporary variable for it. */
return 0;
}
--
What |Removed |Added
----------------------------------------------------------------------------
Status|RESOLVED |UNCONFIRMED
Resolution|INVALID |
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20547
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/20547] undefined reference to "static const" fields of classes
2005-03-19 6:59 [Bug c++/20547] New: " Hu dot YuehWei at gmail dot com
` (4 preceding siblings ...)
2005-03-19 14:16 ` Hu dot YuehWei at gmail dot com
@ 2005-03-19 14:34 ` pinskia at gcc dot gnu dot org
2005-03-19 18:26 ` Hu dot YuehWei at gmail dot com
` (3 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-03-19 14:34 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From pinskia at gcc dot gnu dot org 2005-03-19 14:34 -------
(In reply to comment #5)
> I understand what you mean.
> But why the following codes works:
>
> #include <iostream>
> #include <vector>
>
> struct T
> {
> static char const a = 3;
> };
>
> std::vector<char> ddd;
>
> int
> main()
> {
> ddd.push_back(static_cast<char>(T::a)); // <========= here
>
> std::cerr << ddd.front() << std::endl;
>
> return 0;
> }
>
> and if I pass an constant to a function which takes its address,
Because you changed an lvalue to a rvalue.
> then why the folloing codes doesn't produce errors?
Because constants are rvalues so there is going to be a temporary variable made for you.
--
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution| |INVALID
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20547
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/20547] undefined reference to "static const" fields of classes
2005-03-19 6:59 [Bug c++/20547] New: " Hu dot YuehWei at gmail dot com
` (3 preceding siblings ...)
2005-03-19 14:03 ` pinskia at gcc dot gnu dot org
@ 2005-03-19 14:16 ` Hu dot YuehWei at gmail dot com
2005-03-19 14:34 ` pinskia at gcc dot gnu dot org
` (4 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: Hu dot YuehWei at gmail dot com @ 2005-03-19 14:16 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From Hu dot YuehWei at gmail dot com 2005-03-19 14:15 -------
I understand what you mean.
But why the following codes works:
#include <iostream>
#include <vector>
struct T
{
static char const a = 3;
};
std::vector<char> ddd;
int
main()
{
ddd.push_back(static_cast<char>(T::a)); // <========= here
std::cerr << ddd.front() << std::endl;
return 0;
}
and if I pass an constant to a function which takes its address,
then why the folloing codes doesn't produce errors?
void
fff(char const &a)
{
}
int
main()
{
fff(4);
return 0;
}
--
What |Removed |Added
----------------------------------------------------------------------------
Status|RESOLVED |UNCONFIRMED
Resolution|INVALID |
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20547
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/20547] undefined reference to "static const" fields of classes
2005-03-19 6:59 [Bug c++/20547] New: " Hu dot YuehWei at gmail dot com
` (2 preceding siblings ...)
2005-03-19 14:01 ` Hu dot YuehWei at gmail dot com
@ 2005-03-19 14:03 ` pinskia at gcc dot gnu dot org
2005-03-19 14:16 ` Hu dot YuehWei at gmail dot com
` (5 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-03-19 14:03 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From pinskia at gcc dot gnu dot org 2005-03-19 14:03 -------
(In reply to comment #2)
> But according the standard, 'const static' data members of an intergral type can
> now be initialized _inside_ their class. In this case, the initialization is
> _also_ a definition, so _no_ further definitions are reuquired outside the class
> body.
No it is not a definition, a definition (or is it the other way around). Still this is invalid. Yes the compiler
is allowed to "optimize" the constant which is why it worked in 2.95.3 (well because the defintion of
push_back was wrong).
Oh, one more thing, the vector code is equivalent to (so you take the address):
struct T
{
static char const a = 3;
};
void
fff(const char &a){ }
int
main()
{
fff(T::a);
return 0;
}
--
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution| |INVALID
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20547
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/20547] undefined reference to "static const" fields of classes
2005-03-19 6:59 [Bug c++/20547] New: " Hu dot YuehWei at gmail dot com
2005-03-19 13:45 ` [Bug c++/20547] " pinskia at gcc dot gnu dot org
2005-03-19 13:53 ` Hu dot YuehWei at gmail dot com
@ 2005-03-19 14:01 ` Hu dot YuehWei at gmail dot com
2005-03-19 14:03 ` pinskia at gcc dot gnu dot org
` (6 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: Hu dot YuehWei at gmail dot com @ 2005-03-19 14:01 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From Hu dot YuehWei at gmail dot com 2005-03-19 14:01 -------
#include <vector>
struct T
{
static char const a = 3;
};
void
fff(char a)
{
}
std::vector<char> b;
int
main()
{
fff(T::a); /* this line of codes is fine. */
b.push_back(T::a); /* however, this line of codes is an error. */
return 0;
}
--
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20547
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/20547] undefined reference to "static const" fields of classes
2005-03-19 6:59 [Bug c++/20547] New: " Hu dot YuehWei at gmail dot com
2005-03-19 13:45 ` [Bug c++/20547] " pinskia at gcc dot gnu dot org
@ 2005-03-19 13:53 ` Hu dot YuehWei at gmail dot com
2005-03-19 14:01 ` Hu dot YuehWei at gmail dot com
` (7 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: Hu dot YuehWei at gmail dot com @ 2005-03-19 13:53 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From Hu dot YuehWei at gmail dot com 2005-03-19 13:53 -------
But according the standard, 'const static' data members of an intergral type can
now be initialized _inside_ their class. In this case, the initialization is
_also_ a definition, so _no_ further definitions are reuquired outside the class
body.
--
What |Removed |Added
----------------------------------------------------------------------------
Status|RESOLVED |UNCONFIRMED
Resolution|INVALID |
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20547
^ permalink raw reply [flat|nested] 11+ messages in thread
* [Bug c++/20547] undefined reference to "static const" fields of classes
2005-03-19 6:59 [Bug c++/20547] New: " Hu dot YuehWei at gmail dot com
@ 2005-03-19 13:45 ` pinskia at gcc dot gnu dot org
2005-03-19 13:53 ` Hu dot YuehWei at gmail dot com
` (8 subsequent siblings)
9 siblings, 0 replies; 11+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2005-03-19 13:45 UTC (permalink / raw)
To: gcc-bugs
------- Additional Comments From pinskia at gcc dot gnu dot org 2005-03-19 13:45 -------
No you need to add the following to your code:
const char T::a;
Since you don't supply the memory location otherwise (which is required by the standard).
--
What |Removed |Added
----------------------------------------------------------------------------
Status|UNCONFIRMED |RESOLVED
Resolution| |INVALID
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=20547
^ permalink raw reply [flat|nested] 11+ messages in thread
end of thread, other threads:[~2006-09-25 21:11 UTC | newest]
Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <bug-20547-10303@http.gcc.gnu.org/bugzilla/>
2006-09-25 21:11 ` [Bug c++/20547] undefined reference to "static const" fields of classes pinskia at gcc dot gnu dot org
2005-03-19 6:59 [Bug c++/20547] New: " Hu dot YuehWei at gmail dot com
2005-03-19 13:45 ` [Bug c++/20547] " pinskia at gcc dot gnu dot org
2005-03-19 13:53 ` Hu dot YuehWei at gmail dot com
2005-03-19 14:01 ` Hu dot YuehWei at gmail dot com
2005-03-19 14:03 ` pinskia at gcc dot gnu dot org
2005-03-19 14:16 ` Hu dot YuehWei at gmail dot com
2005-03-19 14:34 ` pinskia at gcc dot gnu dot org
2005-03-19 18:26 ` Hu dot YuehWei at gmail dot com
2005-03-19 18:36 ` pinskia at gcc dot gnu dot org
2005-03-20 7:59 ` Hu dot YuehWei at gmail dot com
2005-03-20 13:55 ` lerdsuwa at gcc dot gnu dot org
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).