-1

I created this struct for some easy templated Math definitions:

template<class T = float> struct Math{
    static constexpr T PI = T(3.14159265359);
    static constexpr T E = T(2.718281828459);
    static constexpr T INF = std::numeric_limits<T>::infinity();
};

I would like to use it like this:

float pi = Math::PI;

Even if the default argument T is float Ill get the error:

'template<class T> struct Math' used without template parameters

If I use Math<>::PI it works. Is this a compiler bug or are the <> brackets mandatory?

tly
  • 1,202
  • 14
  • 17

3 Answers3

1

Yes the <> brackets are mandatory (see here).

But here are some other options:

use a typedef

typedef Math<> MyDefaultMath
// or
typedef Math<float> MyFloatMath

or just drop the template

struct Math
{
    static constexpr float PI = 3.14159265359f;
    // ...
Community
  • 1
  • 1
Axalo
  • 2,953
  • 4
  • 25
  • 39
1

Is this a compiler bug or are the <> brackets mandatory?

No, it's not a bug they are mandatory.

However, you may want to give this a little more thought. What if someone uses Math<int>? For example the documentation of std::numeric_limits::infinity states the following:

Only meaningful if std::numeric_limits::has_infinity == true

James Adkison
  • 9,412
  • 2
  • 29
  • 43
  • Thanks, this is ofcourse true. I would have used the template mainly for `float` and `double`. – tly Apr 25 '15 at 13:28
1

The empty brackets are mandatory and required by the standard. From [temp.arg], emphasis mine:

When template argument packs or default template-arguments are used, a template-argument list can be empty. In that case the empty <> brackets shall still be used as the template-argument-list. [ Example:

template<class T = char> class String;
String<>* p; // OK: String<char>
String* q; // syntax error

template<class ... Elements> class Tuple;
Tuple<>* t; // OK: Elements is empty
Tuple* u; // syntax error

—end example ]

Barry
  • 286,269
  • 29
  • 621
  • 977