1

I have an error

limits:30:29: error: 'numeric_limits' is not a template

in the file, which overloads std::numeric_limits for a specific classes:

  // file "limits"
#include <limits>

namespace std
{
template<typename T> struct numeric_limits<XX::YY<T> > : public 
                            numeric_limits<typename XX::YY<T>::ZZ>
{
private:
<...>

what can be a problem?

klm123
  • 12,105
  • 14
  • 57
  • 95
  • You can find a fix in duplicate https://stackoverflow.com/questions/68081481/how-can-i-build-qt-5-13-2-with-gcc-11-1-on-windows – Nelson Feb 13 '22 at 22:07

3 Answers3

3

You're trying to create a std::numeric_limits that inherits from std::numeric_limits -- but by the time you get to the public numeric_limits... part, you've already declared your own template (that's still incomplete) that's already named numeric_limits, so it's trying to inherit from itself instead of the existing std::numeric_limits.

std::numeric_limits isn't intended as a base class, and doesn't provide any virtual functions, so inheriting from it isn't useful anyway. To make numeric_limits handle your particular class correctly, you want to define a specialization of numeric_limits for that type:

#include <limits> // get base template definition + standard specializations

namespace std { 

template<>        // define your specialization
class numeric_limits<MyType> {
// ...
};

}

Note that this is one of the only cases where you're allowed to add something to the std namespace -- adding a new specialization of an existing template over a user defined type.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • numeric_limits is a of type `struct` not `class`. It extends `public __numeric_limits_base` which is also a `struct` – gifnoc-gkp Aug 15 '13 at 19:17
  • 2
    @TheOtherGuy - doesn't matter whether its labeled `struct` or `class`; that **only** affects default access. In fact, you can declare `struct X;` and define it as `class X { ... };` and vice versa. The rest of your comment seems to be internal details for some particular implementation. – Pete Becker Aug 15 '13 at 19:45
  • @PeteBecker I knew that `structs` have their members set to `public` by default. I didn't know that you could declare `struct` and define it as `class`, I never expected it to be allowed! Thanks! – gifnoc-gkp Aug 15 '13 at 19:48
  • @TheOtherGuy - just for completeness, in addition, a `struct` inherits publicly by default and a class inherits privately by default. – Pete Becker Aug 15 '13 at 19:50
2

Your file shouldn't be named limits like that, as it prevents the inclusion of the normal system header. That, and I'm not even sure if it's legal to redefine the headers.

Personally, I would put this definition in the same header file as XX::YY

Dave S
  • 20,507
  • 3
  • 48
  • 68
0
  1. The numeric_limits doesn't try to inherit itself.

  2. The file can be named "limits", until it is not in the include path.

The problem was that "limits" was in the include path. So it tried to include itself, not std-limits. As result no numeric_limits was declared at the beginning of the file at all.

I excluded the directory from include path and everything works nicely.

klm123
  • 12,105
  • 14
  • 57
  • 95