This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/52036] New: C++11 allows template parameters to have internal linkage


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;
}


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]