public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/36261]  New: Static const member data not found when used in a template
@ 2008-05-18 22:21 Bernd dot Donner at gmx dot net
  2008-05-18 22:53 ` [Bug c++/36261] " pinskia at gcc dot gnu dot org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: Bernd dot Donner at gmx dot net @ 2008-05-18 22:21 UTC (permalink / raw)
  To: gcc-bugs

This small program cannot be compiled. It failes with an "undefined reference
to `A::N'" error. Replacing the main function line with:
std::cout << "Number: " << std::min(1, (int)A::N) << "\n";
results in a successful compilation.

This is the small program to reproduce the bug:


#include <iostream>
#include <algorithm>


struct A {
  static const int N = 2;
};


int main ()
{
  std::cout << "Number: " << std::min(1, A::N) << "\n";
}


-- 
           Summary: Static const member data not found when used in a
                    template
           Product: gcc
           Version: 4.3.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: Bernd dot Donner at gmx dot net


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36261


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/36261] Static const member data not found when used in a template
  2008-05-18 22:21 [Bug c++/36261] New: Static const member data not found when used in a template Bernd dot Donner at gmx dot net
@ 2008-05-18 22:53 ` pinskia at gcc dot gnu dot org
  2008-05-18 22:59 ` paolo dot carlini at oracle dot com
                   ` (4 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2008-05-18 22:53 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2008-05-18 22:52 -------
std::min(1, A::N)

std::min takes a reference to a const type which means you need the defintion
of A::N including the definition.


-- 

pinskia at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36261


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/36261] Static const member data not found when used in a template
  2008-05-18 22:21 [Bug c++/36261] New: Static const member data not found when used in a template Bernd dot Donner at gmx dot net
  2008-05-18 22:53 ` [Bug c++/36261] " pinskia at gcc dot gnu dot org
@ 2008-05-18 22:59 ` paolo dot carlini at oracle dot com
  2008-05-19  0:01 ` Bernd dot Donner at gmx dot net
                   ` (3 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: paolo dot carlini at oracle dot com @ 2008-05-18 22:59 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from paolo dot carlini at oracle dot com  2008-05-18 22:58 -------
In other terms, just add, after the declaration of struct A:

const int A::N;


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36261


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/36261] Static const member data not found when used in a template
  2008-05-18 22:21 [Bug c++/36261] New: Static const member data not found when used in a template Bernd dot Donner at gmx dot net
  2008-05-18 22:53 ` [Bug c++/36261] " pinskia at gcc dot gnu dot org
  2008-05-18 22:59 ` paolo dot carlini at oracle dot com
@ 2008-05-19  0:01 ` Bernd dot Donner at gmx dot net
  2008-05-19  0:11 ` paolo dot carlini at oracle dot com
                   ` (2 subsequent siblings)
  5 siblings, 0 replies; 7+ messages in thread
From: Bernd dot Donner at gmx dot net @ 2008-05-19  0:01 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from Bernd dot Donner at gmx dot net  2008-05-19 00:00 -------
(In reply to comment #2)
> In other terms, just add, after the declaration of struct A:
> 
> const int A::N;
> 

According to the C++ standard "static const" member variables can be declarend
_and_ defined immediately inside the declaration of the class. It should not be
neccessary to declare A::N outside the class again. Other compilers "microsoft
express 2005" by the way do accept this code. The definition of A::N is
present. Otherwise, why does this code compile and link fine?

This program is accepted by gcc:

#include <iostream>
#include <algorithm>


struct A {
  static const int N = 2;
};


int main ()
{
  std::cout << "Number: " << A::N << "\n";
}

This program is _not_ accepted by gcc:

#include <iostream>
#include <algorithm>


struct A {
  static const int N = 2;
};


int main ()
{
  std::cout << "Number: " << std::min(1, A::N) << "\n";
}



Static const member variables may be defined and declared the way I have
proposed. According to the standard it is no more neccessary to provide a
declaration outside the class, I know that this was once neccessary. The web is
full of examples that shows that my declaration and definition of A::N is legal
in C++. Sorry, for insisting to look at this problem once again.


-- 

Bernd dot Donner at gmx dot net changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |UNCONFIRMED
         Resolution|INVALID                     |


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36261


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/36261] Static const member data not found when used in a template
  2008-05-18 22:21 [Bug c++/36261] New: Static const member data not found when used in a template Bernd dot Donner at gmx dot net
                   ` (2 preceding siblings ...)
  2008-05-19  0:01 ` Bernd dot Donner at gmx dot net
@ 2008-05-19  0:11 ` paolo dot carlini at oracle dot com
  2008-05-19  0:17 ` paolo dot carlini at oracle dot com
  2008-05-19  0:19 ` Bernd dot Donner at gmx dot net
  5 siblings, 0 replies; 7+ messages in thread
From: paolo dot carlini at oracle dot com @ 2008-05-19  0:11 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from paolo dot carlini at oracle dot com  2008-05-19 00:10 -------


*** This bug has been marked as a duplicate of 30745 ***


-- 

paolo dot carlini at oracle dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|                            |DUPLICATE


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36261


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/36261] Static const member data not found when used in a template
  2008-05-18 22:21 [Bug c++/36261] New: Static const member data not found when used in a template Bernd dot Donner at gmx dot net
                   ` (3 preceding siblings ...)
  2008-05-19  0:11 ` paolo dot carlini at oracle dot com
@ 2008-05-19  0:17 ` paolo dot carlini at oracle dot com
  2008-05-19  0:19 ` Bernd dot Donner at gmx dot net
  5 siblings, 0 replies; 7+ messages in thread
From: paolo dot carlini at oracle dot com @ 2008-05-19  0:17 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from paolo dot carlini at oracle dot com  2008-05-19 00:16 -------
And you are certainly wrong that the Standard allows to omit the definition, in
this case. This is 9.4.2/4 oc C++03, note the last sentence:

If a static data member is of const integral or const enumeration type, its
declaration in the class definition can specify aconstant-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
name-space scope if it is used in the program and the namespace scope
definition shall not contain aninitializer. 


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36261


^ permalink raw reply	[flat|nested] 7+ messages in thread

* [Bug c++/36261] Static const member data not found when used in a template
  2008-05-18 22:21 [Bug c++/36261] New: Static const member data not found when used in a template Bernd dot Donner at gmx dot net
                   ` (4 preceding siblings ...)
  2008-05-19  0:17 ` paolo dot carlini at oracle dot com
@ 2008-05-19  0:19 ` Bernd dot Donner at gmx dot net
  5 siblings, 0 replies; 7+ messages in thread
From: Bernd dot Donner at gmx dot net @ 2008-05-19  0:19 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from Bernd dot Donner at gmx dot net  2008-05-19 00:18 -------
Sorry, this is my fault your comments are correct.


-- 

Bernd dot Donner at gmx dot net changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|DUPLICATE                   |INVALID


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=36261


^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2008-05-19  0:19 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2008-05-18 22:21 [Bug c++/36261] New: Static const member data not found when used in a template Bernd dot Donner at gmx dot net
2008-05-18 22:53 ` [Bug c++/36261] " pinskia at gcc dot gnu dot org
2008-05-18 22:59 ` paolo dot carlini at oracle dot com
2008-05-19  0:01 ` Bernd dot Donner at gmx dot net
2008-05-19  0:11 ` paolo dot carlini at oracle dot com
2008-05-19  0:17 ` paolo dot carlini at oracle dot com
2008-05-19  0:19 ` Bernd dot Donner at gmx dot net

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).