public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/52036] New: C++11 allows template parameters to have internal linkage
@ 2012-01-29  8:52 benjamin at benkay dot net
  2012-01-29 14:33 ` [Bug c++/52036] " daniel.kruegler at googlemail dot com
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: benjamin at benkay dot net @ 2012-01-29  8:52 UTC (permalink / raw)
  To: gcc-bugs

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

             Bug #: 52036
           Summary: C++11 allows template parameters to have internal
                    linkage
    Classification: Unclassified
           Product: gcc
           Version: 4.6.2
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: benjamin@benkay.net


First gcc bug report, so please be kind :-). When using pointers or references
as template parameters, C++03 requires these to have external linkage. I think
(and could be wrong) that C++11 relaxes this restriction. I would expect the
attached code to compile with g++ -std=c++0x, but it does not.

testcase.cpp:
template <typename T, const T &val> class TestClass
{
public:
  TestClass() : _val(val) { }
private:
  T _val;
};

extern constexpr float e = 2.72;

int main()
{
  constexpr float pi = 3.14;

  /* Allowed in both C++03 and C++11. */
  TestClass<float, e> test1;

  /* Not allowed in C++03 because pi lacks external linkage...
   * But isn't this allowed in C++11? */
  TestClass<float, pi> test2;

  return 0;
}

$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6/lto-wrapper
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Debian 4.6.2-12'
--with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++,go --prefix=/usr
--program-suffix=-4.6 --enable-shared --enable-linker-build-id
--with-system-zlib --libexecdir=/usr/lib --without-included-gettext
--enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6
--libdir=/usr/lib --enable-nls --enable-clocale=gnu --enable-libstdcxx-debug
--enable-libstdcxx-time=yes --enable-plugin --enable-objc-gc
--with-arch-32=i586 --with-tune=generic --enable-checking=release
--build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
Thread model: posix
gcc version 4.6.2 (Debian 4.6.2-12)

$ g++ -std=c++0x -Wall -save-temps testcase.cpp
testcase.cpp: In function ‘int main()’:
testcase.cpp:20:22: error: ‘pi’ is not a valid template argument for type
‘const float&’ because object ‘pi’ has not external linkage
testcase.cpp:20:29: error: invalid type in declaration before ‘;’ token
testcase.cpp:20:24: warning: unused variable ‘test2’ [-Wunused-variable]

testcase.ii:
# 1 "testcase.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "testcase.cpp"
template <typename T, const T &val> class TestClass
{
public:
  TestClass() : _val(val) { }
private:
  T _val;
};

extern constexpr float e = 2.72;

int main()
{
  constexpr float pi = 3.14;


  TestClass<float, e> test1;



  TestClass<float, pi> test2;

  return 0;
}


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

* [Bug c++/52036] C++11 allows template parameters to have internal linkage
  2012-01-29  8:52 [Bug c++/52036] New: C++11 allows template parameters to have internal linkage benjamin at benkay dot net
@ 2012-01-29 14:33 ` daniel.kruegler at googlemail dot com
  2012-01-29 14:42 ` paolo.carlini at oracle dot com
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: daniel.kruegler at googlemail dot com @ 2012-01-29 14:33 UTC (permalink / raw)
  To: gcc-bugs

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

Daniel Krügler <daniel.kruegler at googlemail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |daniel.kruegler at
                   |                            |googlemail dot com

--- Comment #1 from Daniel Krügler <daniel.kruegler at googlemail dot com> 2012-01-29 13:59:50 UTC ---
Yes, C++11 has relaxed the constraints and supports variables of internal
linkage as well. E.g. a variable declared with the static keyword in namespace
scope is feasible now. But your example does not provide a variable of either
internal or external linkage: Variable pi is an automatic variable and thus has
*no* linkage at all. Those are no valid template arguments in C++11 either.
Without any form of linkage, a compiler cannot associate an address constant or
reference constant with the variable.


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

* [Bug c++/52036] C++11 allows template parameters to have internal linkage
  2012-01-29  8:52 [Bug c++/52036] New: C++11 allows template parameters to have internal linkage benjamin at benkay dot net
  2012-01-29 14:33 ` [Bug c++/52036] " daniel.kruegler at googlemail dot com
@ 2012-01-29 14:42 ` paolo.carlini at oracle dot com
  2012-01-29 17:27 ` benjamin at benkay dot net
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: paolo.carlini at oracle dot com @ 2012-01-29 14:42 UTC (permalink / raw)
  To: gcc-bugs

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

Paolo Carlini <paolo.carlini at oracle dot com> changed:

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

--- Comment #2 from Paolo Carlini <paolo.carlini at oracle dot com> 2012-01-29 14:08:18 UTC ---
Closing.


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

* [Bug c++/52036] C++11 allows template parameters to have internal linkage
  2012-01-29  8:52 [Bug c++/52036] New: C++11 allows template parameters to have internal linkage benjamin at benkay dot net
  2012-01-29 14:33 ` [Bug c++/52036] " daniel.kruegler at googlemail dot com
  2012-01-29 14:42 ` paolo.carlini at oracle dot com
@ 2012-01-29 17:27 ` benjamin at benkay dot net
  2012-01-29 18:15 ` daniel.kruegler at googlemail dot com
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: benjamin at benkay dot net @ 2012-01-29 17:27 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Benjamin Kay <benjamin at benkay dot net> 2012-01-29 16:24:29 UTC ---
Oops! You're quite right, pi is automatic in my test case. However compilation
still fails if pi is given internal linkage by declaring it static, i.e.

int main()
{
  static constexpr float pi = 3.14;

Or even:

namespace {
  static constexpr float pi = 3.14;
}
int main()
{

Can you provide an example of a variable with internal linkage being used as a
reference template parameter that compiles using g++?


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

* [Bug c++/52036] C++11 allows template parameters to have internal linkage
  2012-01-29  8:52 [Bug c++/52036] New: C++11 allows template parameters to have internal linkage benjamin at benkay dot net
                   ` (2 preceding siblings ...)
  2012-01-29 17:27 ` benjamin at benkay dot net
@ 2012-01-29 18:15 ` daniel.kruegler at googlemail dot com
  2012-01-29 19:01 ` paolo.carlini at oracle dot com
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: daniel.kruegler at googlemail dot com @ 2012-01-29 18:15 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Daniel Krügler <daniel.kruegler at googlemail dot com> 2012-01-29 17:28:14 UTC ---
(In reply to comment #3)
> However compilation still fails if pi is given internal linkage by declaring it > static, i.e.
> 
> int main()
> {
>   static constexpr float pi = 3.14;
> 
> Or even:
> 
> namespace {
>   static constexpr float pi = 3.14;
> }

I agree that both examples should work and that the lack of that is a bug when
compiled in C++11 mode. Paolo, this bug should be reopened.


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

* [Bug c++/52036] C++11 allows template parameters to have internal linkage
  2012-01-29  8:52 [Bug c++/52036] New: C++11 allows template parameters to have internal linkage benjamin at benkay dot net
                   ` (3 preceding siblings ...)
  2012-01-29 18:15 ` daniel.kruegler at googlemail dot com
@ 2012-01-29 19:01 ` paolo.carlini at oracle dot com
  2012-01-29 22:51 ` redi at gcc dot gnu.org
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: paolo.carlini at oracle dot com @ 2012-01-29 19:01 UTC (permalink / raw)
  To: gcc-bugs

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

Paolo Carlini <paolo.carlini at oracle dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|RESOLVED                    |REOPENED
   Last reconfirmed|                            |2012-01-29
         Resolution|INVALID                     |
     Ever Confirmed|0                           |1

--- Comment #5 from Paolo Carlini <paolo.carlini at oracle dot com> 2012-01-29 18:31:44 UTC ---
Let's re-open this, but we are talking about a different testcase.

Note, the feature may well be knowingly in unimplemented status and for sure we
don't have a Bugzilla PR for each unimplemented C++11 feature.


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

* [Bug c++/52036] C++11 allows template parameters to have internal linkage
  2012-01-29  8:52 [Bug c++/52036] New: C++11 allows template parameters to have internal linkage benjamin at benkay dot net
                   ` (4 preceding siblings ...)
  2012-01-29 19:01 ` paolo.carlini at oracle dot com
@ 2012-01-29 22:51 ` redi at gcc dot gnu.org
  2012-01-30 20:19 ` daniel.kruegler at googlemail dot com
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2012-01-29 22:51 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> 2012-01-29 22:25:02 UTC ---
At least we should change "has not external linkage" to proper English, that's
annoyed me for some time ;)


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

* [Bug c++/52036] C++11 allows template parameters to have internal linkage
  2012-01-29  8:52 [Bug c++/52036] New: C++11 allows template parameters to have internal linkage benjamin at benkay dot net
                   ` (5 preceding siblings ...)
  2012-01-29 22:51 ` redi at gcc dot gnu.org
@ 2012-01-30 20:19 ` daniel.kruegler at googlemail dot com
  2013-08-09 23:54 ` richard-gccbugzilla at metafoo dot co.uk
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: daniel.kruegler at googlemail dot com @ 2012-01-30 20:19 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Daniel Krügler <daniel.kruegler at googlemail dot com> 2012-01-30 19:28:05 UTC ---
(In reply to comment #4)
> (In reply to comment #3)
> > However compilation still fails if pi is given internal linkage by declaring it > static, i.e.
> > 
> > int main()
> > {
> >   static constexpr float pi = 3.14;
> > 
> > Or even:
> > 
> > namespace {
> >   static constexpr float pi = 3.14;
> > }
> 
> I agree that both examples should work and that the lack of that is a bug when
> compiled in C++11 mode. Paolo, this bug should be reopened.

Partial correction: The first example involving a function local static has no
linkage (and should not be accepted), but the second example does.


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

* [Bug c++/52036] C++11 allows template parameters to have internal linkage
  2012-01-29  8:52 [Bug c++/52036] New: C++11 allows template parameters to have internal linkage benjamin at benkay dot net
                   ` (6 preceding siblings ...)
  2012-01-30 20:19 ` daniel.kruegler at googlemail dot com
@ 2013-08-09 23:54 ` richard-gccbugzilla at metafoo dot co.uk
  2014-03-03 22:57 ` mizvekov at gmail dot com
  2015-03-23  7:52 ` oakad at yahoo dot com
  9 siblings, 0 replies; 11+ messages in thread
From: richard-gccbugzilla at metafoo dot co.uk @ 2013-08-09 23:54 UTC (permalink / raw)
  To: gcc-bugs

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

Richard Smith <richard-gccbugzilla at metafoo dot co.uk> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |richard-gccbugzilla@metafoo
                   |                            |.co.uk

--- Comment #8 from Richard Smith <richard-gccbugzilla at metafoo dot co.uk> ---
C++11 [temp.arg.nontype]p2 has another similar testcase, which GCC fails on:

template<class T, const char* p> class X {
  /* ... */
};
X<int, "Studebaker"> x1; // error: string literal as template-argument

const char p[] = "Vivisectionist";
X<int,p> x2;             // OK


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

* [Bug c++/52036] C++11 allows template parameters to have internal linkage
  2012-01-29  8:52 [Bug c++/52036] New: C++11 allows template parameters to have internal linkage benjamin at benkay dot net
                   ` (7 preceding siblings ...)
  2013-08-09 23:54 ` richard-gccbugzilla at metafoo dot co.uk
@ 2014-03-03 22:57 ` mizvekov at gmail dot com
  2015-03-23  7:52 ` oakad at yahoo dot com
  9 siblings, 0 replies; 11+ messages in thread
From: mizvekov at gmail dot com @ 2014-03-03 22:57 UTC (permalink / raw)
  To: gcc-bugs

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

Matheus Izvekov <mizvekov at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mizvekov at gmail dot com

--- Comment #9 from Matheus Izvekov <mizvekov at gmail dot com> ---
Still an issue on GCC 4.8.2


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

* [Bug c++/52036] C++11 allows template parameters to have internal linkage
  2012-01-29  8:52 [Bug c++/52036] New: C++11 allows template parameters to have internal linkage benjamin at benkay dot net
                   ` (8 preceding siblings ...)
  2014-03-03 22:57 ` mizvekov at gmail dot com
@ 2015-03-23  7:52 ` oakad at yahoo dot com
  9 siblings, 0 replies; 11+ messages in thread
From: oakad at yahoo dot com @ 2015-03-23  7:52 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=52036

Alexander Dubov <oakad at yahoo dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |oakad at yahoo dot com

--- Comment #10 from Alexander Dubov <oakad at yahoo dot com> ---
Still an issue on GCC-4.9.2 (-std=c++11, c++14, gnu++1y). clang-3.4 compiles
this and similar cases just fine.


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

end of thread, other threads:[~2015-03-23  4:55 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-01-29  8:52 [Bug c++/52036] New: C++11 allows template parameters to have internal linkage benjamin at benkay dot net
2012-01-29 14:33 ` [Bug c++/52036] " daniel.kruegler at googlemail dot com
2012-01-29 14:42 ` paolo.carlini at oracle dot com
2012-01-29 17:27 ` benjamin at benkay dot net
2012-01-29 18:15 ` daniel.kruegler at googlemail dot com
2012-01-29 19:01 ` paolo.carlini at oracle dot com
2012-01-29 22:51 ` redi at gcc dot gnu.org
2012-01-30 20:19 ` daniel.kruegler at googlemail dot com
2013-08-09 23:54 ` richard-gccbugzilla at metafoo dot co.uk
2014-03-03 22:57 ` mizvekov at gmail dot com
2015-03-23  7:52 ` oakad at yahoo 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).