public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/44548]  New: Link error when defining templated static const variable
@ 2010-06-15 18:57 dpovey at gmail dot com
  2010-06-15 19:01 ` [Bug c++/44548] " pinskia at gcc dot gnu dot org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: dpovey at gmail dot com @ 2010-06-15 18:57 UTC (permalink / raw)
  To: gcc-bugs

Complete code is below [just a few lines].  Note-- bug goes away when I
initialize the variable outside the class, not inside.

qpovey@merlin: ~$ cat temp.cc

template<class T=int> class MyTraits {
 public:
  static const T kValue = 0;
};

template<>
const int MyTraits<int>::kValue; // define it.

int main(){
  const void * a = &(MyTraits<int>::kValue);
}

qpovey@merlin: ~$ g++ temp.cc
/tmp/ccuYrD0D.o: In function `main':
temp.cc:(.text+0x14): undefined reference to `MyTraits<int>::kValue'
collect2: ld returned 1 exit status
qpovey@merlin: ~$ g++ -v
Using built-in specs.
Target: i686-linux
Configured with: ../configure --build=i686-linux --with-arch=nocona
--with-tune=core2 --with-thread=posix --with-as=/usr/local/bin/as
--with-ld=/usr/local/bin/ld --with-system-zlib --program-suffix=-4.3
Thread model: posix
gcc version 4.3.5 (GCC) 
qpovey@merlin: ~$ uname -a
Linux merlin.fit.vutbr.cz 2.6.32.12 #1 SMP Tue Apr 27 15:10:42 CEST 2010 x86_64
x86_64 x86_64 GNU/Linux
qpovey@merlin: ~$


-- 
           Summary: Link error when defining templated static const variable
           Product: gcc
           Version: 4.3.5
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: dpovey at gmail dot com
 GCC build triplet: i686-linux-gnu
  GCC host triplet: i686-linux-gnu
GCC target triplet: i686-linux-gnu


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


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

* [Bug c++/44548] Link error when defining templated static const variable
  2010-06-15 18:57 [Bug c++/44548] New: Link error when defining templated static const variable dpovey at gmail dot com
@ 2010-06-15 19:01 ` pinskia at gcc dot gnu dot org
  2010-06-15 22:19 ` dpovey at gmail dot com
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2010-06-15 19:01 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #1 from pinskia at gcc dot gnu dot org  2010-06-15 19:00 -------
No you just specialized the declaration.
You want:
template<class T>
const T MyTraits<T>::kValue; // define it.


-- 

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=44548


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

* [Bug c++/44548] Link error when defining templated static const variable
  2010-06-15 18:57 [Bug c++/44548] New: Link error when defining templated static const variable dpovey at gmail dot com
  2010-06-15 19:01 ` [Bug c++/44548] " pinskia at gcc dot gnu dot org
@ 2010-06-15 22:19 ` dpovey at gmail dot com
  2010-06-15 22:24 ` pinskia at gcc dot gnu dot org
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: dpovey at gmail dot com @ 2010-06-15 22:19 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #2 from dpovey at gmail dot com  2010-06-15 22:19 -------
I don't agree with you that this is not a bug, although I do agree that I could
have coded it differently.

Look at

http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/explicit_specialization.htm

the section on  "Explicit specialization of members of class templates".

You are right, my syntax was an explicit specialization, and that was pointless
and I could have done it a different way, but I believe what I wrote still
counts as  a definition of the variable-- what else could the syntax possibly
mean?  I.e. the line:
template<> const int MyTraits<int>::kValue;
You say this is an explicit specialization; fine, but it is an explicit
specialization of the *definition* of the variable. You can't *declare* a class
member outside of the class itself, and I'm not specializing the class itself. 
So either that line needs to be rejected by the compiler, or it needs to work
as I intended-- what other possible meaning could it have than the one I
intended?  The C++ standard doesn't seem to mention whether or not you are
allowed to explicitly specialize static const variables that have been defined
inside the class, and it does seem a bit pointless to do so, but I think it
should either compile or issue an error or warning. 


-- 

dpovey at gmail dot com changed:

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


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


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

* [Bug c++/44548] Link error when defining templated static const variable
  2010-06-15 18:57 [Bug c++/44548] New: Link error when defining templated static const variable dpovey at gmail dot com
  2010-06-15 19:01 ` [Bug c++/44548] " pinskia at gcc dot gnu dot org
  2010-06-15 22:19 ` dpovey at gmail dot com
@ 2010-06-15 22:24 ` pinskia at gcc dot gnu dot org
  2010-06-16 11:59 ` manu at gcc dot gnu dot org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu dot org @ 2010-06-15 22:24 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #3 from pinskia at gcc dot gnu dot org  2010-06-15 22:24 -------
> but it is an explicit specialization of the *definition* of the variable

No it is a specialization of the declaration.  There are only specialization of
declarations; never definitions.  Re-read the link you gave and you will see
the following: "Explicit specialization declaration syntax" .


-- 

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=44548


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

* [Bug c++/44548] Link error when defining templated static const variable
  2010-06-15 18:57 [Bug c++/44548] New: Link error when defining templated static const variable dpovey at gmail dot com
                   ` (2 preceding siblings ...)
  2010-06-15 22:24 ` pinskia at gcc dot gnu dot org
@ 2010-06-16 11:59 ` manu at gcc dot gnu dot org
  2010-06-16 12:12 ` redi at gcc dot gnu dot org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: manu at gcc dot gnu dot org @ 2010-06-16 11:59 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #4 from manu at gcc dot gnu dot org  2010-06-16 11:58 -------
(In reply to comment #3)
> > but it is an explicit specialization of the *definition* of the variable
> 
> No it is a specialization of the declaration.  There are only specialization of
> declarations; never definitions.  Re-read the link you gave and you will see
> the following: "Explicit specialization declaration syntax" .

So what the above code actually means? Is it valid?


-- 

manu at gcc dot gnu dot org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |manu at gcc dot gnu dot org


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


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

* [Bug c++/44548] Link error when defining templated static const variable
  2010-06-15 18:57 [Bug c++/44548] New: Link error when defining templated static const variable dpovey at gmail dot com
                   ` (3 preceding siblings ...)
  2010-06-16 11:59 ` manu at gcc dot gnu dot org
@ 2010-06-16 12:12 ` redi at gcc dot gnu dot org
  2010-06-16 12:31 ` redi at gcc dot gnu dot org
  2010-06-16 17:58 ` dpovey at gmail dot com
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu dot org @ 2010-06-16 12:12 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #5 from redi at gcc dot gnu dot org  2010-06-16 12:11 -------
(In reply to comment #4) 
> So what the above code actually means? Is it valid?

Yes, it's valid. It declares an explicit specialization, which inhibits the
implicit instantiation of the default specialization.

In order for the code to be valid a definition must be provided. It isn't, so
you get a link error.


-- 


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


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

* [Bug c++/44548] Link error when defining templated static const variable
  2010-06-15 18:57 [Bug c++/44548] New: Link error when defining templated static const variable dpovey at gmail dot com
                   ` (4 preceding siblings ...)
  2010-06-16 12:12 ` redi at gcc dot gnu dot org
@ 2010-06-16 12:31 ` redi at gcc dot gnu dot org
  2010-06-16 17:58 ` dpovey at gmail dot com
  6 siblings, 0 replies; 8+ messages in thread
From: redi at gcc dot gnu dot org @ 2010-06-16 12:31 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #6 from redi at gcc dot gnu dot org  2010-06-16 12:31 -------
(In reply to comment #3)
> > but it is an explicit specialization of the *definition* of the variable
> 
> No it is a specialization of the declaration.

It is a declaration of a specialization :)

> There are only specialization of declarations; never definitions.

Not true.

This is a definition of an explicit specialization:

template<>
const int MyTraits<int>::kValue = 1;

Such a definition must only occur in a single translation unit, otherwise you
get duplicate symbols.  To use it from multiple translation units you must
declare it in all and define it in one.

Here is a complete example:

template<class T=int> class MyTraits {
 public:
  static const T kValue;
};

// define the default specialization
template<class T>
const T MyTraits<T>::kValue = 0;

// declare an explicit specialization
template<>
const int MyTraits<int>::kValue;

int main(){
  const void * a = &(MyTraits<int>::kValue);
}

// define the explicit specialization
template<>
const int MyTraits<int>::kValue = 1;


-- 


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


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

* [Bug c++/44548] Link error when defining templated static const variable
  2010-06-15 18:57 [Bug c++/44548] New: Link error when defining templated static const variable dpovey at gmail dot com
                   ` (5 preceding siblings ...)
  2010-06-16 12:31 ` redi at gcc dot gnu dot org
@ 2010-06-16 17:58 ` dpovey at gmail dot com
  6 siblings, 0 replies; 8+ messages in thread
From: dpovey at gmail dot com @ 2010-06-16 17:58 UTC (permalink / raw)
  To: gcc-bugs



------- Comment #7 from dpovey at gmail dot com  2010-06-16 17:57 -------
The key thing here is that the value was initialized inside the class.  So
there is no way to syntactically disambiguate a definition and a declaration of
the  value outside the class  (because normally C++ uses the initialization to
tell that you are defining it, but if you initialized it inside the class then
it doesn't let you initialize it again outside the class).  For non-specialized
"declarations/definitions", if they're initialized inside the class C++ is
forced to treat the (declaration/definition) without initializer as a
definition (I guess because it's more important to be able to define it to
declare it).  I assumed that when you specialize it, C++ also treats this
syntax as a definition.  It's possible that I was wrong.  If so I think it's a
misfeature in the C++ standard.  But I really don't know how it's interpreting
it.  

Suppose you do:

template <class A> class C{
  int i = 1;
};
template<> int C<void*>::i;

Let's suppose it treats this as a declaration.  What is it declaring?  There is
no syntax available to *define* the value (because it looks the same as a
declaration).  Conceivably you could later override the class itself, e.g.:

template <> class C<int> {
    int i = 0;
};

But I don't know whether C++ would treat the declaration statement above as
somehow relating to this.


-- 


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


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

end of thread, other threads:[~2010-06-16 17:58 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-06-15 18:57 [Bug c++/44548] New: Link error when defining templated static const variable dpovey at gmail dot com
2010-06-15 19:01 ` [Bug c++/44548] " pinskia at gcc dot gnu dot org
2010-06-15 22:19 ` dpovey at gmail dot com
2010-06-15 22:24 ` pinskia at gcc dot gnu dot org
2010-06-16 11:59 ` manu at gcc dot gnu dot org
2010-06-16 12:12 ` redi at gcc dot gnu dot org
2010-06-16 12:31 ` redi at gcc dot gnu dot org
2010-06-16 17:58 ` dpovey at gmail dot com

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