Why can you implement std::is_function in terms of std::is_const and std::is_reference?
Asked Answered
Y

2

90

How is the following an implementation for std::is_function?

template<class T>
struct is_function : std::integral_constant<
    bool,
    !std::is_const<const T>::value && !std::is_reference<T>::value
> {};

(from CPP Reference)

Seems to me, an int would be a function under this definition. What am I missing?

Yevetteyew answered 8/1, 2020 at 22:6 Comment(4)
Think about the !is_const part.Quinquereme
Why can't function types be const? Is this related to abominable types?Fronton
@Fronton it's because functions are one of the few things in C++ that aren't objects.Mullin
I guess because in a sense it's always constAutolysin
M
79

Let's go over the conditions as they appear:
If const T isn't const (const doesn't really apply to function types since functions aren't objects), and T isn't a reference (const doesn't apply to references either for the same reason), it's a function type. int (or any other non-function-non-reference type) wouldn't fit in because is_const<const int>::value is true.

According to C++17 Standard §11.3.5 Functions / section 7: (Emphasis mine)

The effect of a cv-qualifier-seq in a function declarator is not the same as adding cv-qualification on top of the function type. In the latter case, the cv-qualifiers are ignored. [ Note: A function type that has a cv-qualifier-seq is not a cv-qualified type; there are no cv-qualified function types. — end note ] [...]

Mullin answered 8/1, 2020 at 22:14 Comment(1)
Ah.... I was missing the "const" inside the is_const part of this. That makes sense.Yevetteyew
M
62

There are only two categories of types in the language that cannot have const-qualification: reference types, and function types. So, if const T fails to be a const-qualified type, it means T is either a function type or a reference type. If you can rule out reference types, then you are left with only function types.

Note that a function type that carries a cv-qualifier, such as int(int) const, is not a const-qualified type. It's an example of an "abominable function type", whose only real use is to compose or decompose pointer-to-member-function types. The type int(int) const cannot be obtained by adding const-qualification on top of int(int). Rather, the const applies to the implied object parameter.

Maryannamaryanne answered 8/1, 2020 at 22:21 Comment(1)
could you please provide an example of the use case for int(int) const ?Zerline

© 2022 - 2024 — McMap. All rights reserved.