klm123
klm123

Reputation: 12885

'numeric_limits' is not a template

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?

Upvotes: 1

Views: 2484

Answers (3)

klm123
klm123

Reputation: 12885

  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.

Upvotes: 0

Jerry Coffin
Jerry Coffin

Reputation: 490328

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.

Upvotes: 3

Dave S
Dave S

Reputation: 21123

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

Upvotes: 2

Related Questions