[PATCH] C++ math constants

Ulrich Drepper drepper@gmail.com
Fri Mar 1 04:16:00 GMT 2013

On Thu, Feb 21, 2013 at 12:38 PM, Benjamin De Kosnik <bkoz@redhat.com> wrote:
> Not seeing it.
> Say for:
> #include <iostream>
>   // A class for math constants.
>   template<typename _RealType>
>     struct __math_constants
>     {
>       // Constant @f$ \pi @f$.
>       static constexpr _RealType __pie =
>   3.1415926535897932384626433832795029L; };
> template<class T>
> void print(const T& t) { std::cout << t; }
> int main()
> {
>   print(__math_constants<double>::__pie);
>   return 0;
> }
> I'm not getting any definition, even at -O0.

Even more so: how would an explicit instantiation even work?

Try this simplified code:

template<typename T>
struct a {
  static constexpr T m = T(1);

If you try

  template<> constexpr int a::m<int>;

nothing gets emitted into the object file (this is even with the trunk
gcc).  If I use

  template<> constexpr int a::m<int> = 1;

I get a definition but I have to remove the initialization in the
class definition itself.  If I use

  struct template a<int>;

there is no output in the file as well.

All this makes perfect sense with gcc.  Since the constant will never
be referenced as a variable there is no need for the compiler to emit
a definition.  If the argument is that there has to be one, how would
it be done?

More information about the Gcc-patches mailing list