Fantastic Mr Fox
Fantastic Mr Fox

Reputation: 33924

Why does `std::unary_function` still compile in c++17?

The std::unary_function feature was deprecated in c++11 and deleted in c++17. But with the c++17 compiler flag, this code still compiles:

struct less_than_7 : std::unary_function<int, bool>
{
    bool operator()(int i) const { return i < 7; }
};

Built with g++ -std=c++17 -O0 -Wall -pedantic main.cpp here.

Is a feature deletion optional for a compiler to implement?

Upvotes: 4

Views: 2881

Answers (3)

Lightness Races in Orbit
Lightness Races in Orbit

Reputation: 385295

While it's nice to have standards, and multiple versions of those standards, the reality is that different toolchains have different levels of compliance.

This may involve delays implementing new features, delays implementing changes, or delays removing things.

This is simply a case of the latter.

VS 2019 has removed std::unary_function, but neither libstdc++ nor libc++ has yet. That's just how it is!

Upvotes: 2

MSalters
MSalters

Reputation: 180010

I'm trying to find the appropriate wording, but implementations do have a lot of leeway in adding extra names to std. That's a major reason why you can't - the two names might clash.

In particular, the existing implementation can have helper classes in std. Thus std::unary_function might have become merely a helper template for this particular implementation.

Upvotes: 1

Bathsheba
Bathsheba

Reputation: 234795

Since it's no longer part of the C++17 standard its inclusion in one of your source code files falls into the same category as code that introduces things into std.

In other words, the program behaviour is undefined.

The program working is a manifestation of that undefined behaviour. And perhaps your implementation defines that undefined behaviour. But even so, your code is not portable C++17.

Upvotes: 4

Related Questions