1

From this cppreference link, is_integral has bool as a member function. How to use it? I could understand other members, and am able to code a simple example as follows. But how to use the bool member function?

#include <iostream>
#include <type_traits>
using namespace std;

int main(){
    cout << boolalpha;
    // member constants
    cout << is_integral_v<int> << endl; // prints true

    // member functions
    // operator bool ?
    
    // operator()
    cout << is_integral<float>() << endl; // prints false

    return 0;
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
balu
  • 1,023
  • 12
  • 18
  • related/dupe: https://stackoverflow.com/questions/18539040/how-to-directly-call-conversion-operator/18539328 – NathanOliver Jan 20 '22 at 04:36
  • https://en.cppreference.com/w/cpp/language/cast_operator – Lingxi Jan 20 '22 at 04:38
  • 2
    You're already using it here `is_integral()` implicitly. – cigien Jan 20 '22 at 04:44
  • 2
    Just a heads up stop using : "using namespace". It is very explained very well by Jason Turner, the reasons may surprise you : https://www.youtube.com/watch?v=MZqjl9HEPZ8 – Pepijn Kramer Jan 20 '22 at 05:23
  • @PepijnKramer for the sake of brevity I used that. – balu Jan 20 '22 at 08:44
  • 4
    Brevity is what will kill maintainability in the long run... won't pass my code reviews. My take on it : even in hobby projects keep using recommended practices so they become second nature. And typeing std:: is like what .5 seconds extra work? – Pepijn Kramer Jan 20 '22 at 09:04
  • @PepijnKramer While I agree with you, that video is actually a poor reference. Starting out with "we don't do this because it has been agreed a long time", simply repeating that a lot and relying on conflicting statement from a coding standard doesn't provide much confidence in their teaching skill. The rest might be worth watching, but it has a very ill-written start. – Mast Jan 20 '22 at 11:12
  • 1
    Luckily we have a very old [Stack Overflow question about it](https://stackoverflow.com/q/1265039/1014587), with a top answer that explains why it's littering the global namespace and examples of where it will go wrong if you do it anyway. – Mast Jan 20 '22 at 11:12
  • @Mast probably its not the best start but then again Jason is quite experienced . And his examples on using 3rd party libraries and namespace conflicts are spot on. These problems are real in large projects (I've worked with code bases involving tens of millions of lines of code and I've seen these things happen) – Pepijn Kramer Jan 20 '22 at 11:50

1 Answers1

4

When you call is_integral<float>(), you are actualy calling the operator bool. The following calls the operator() instead:

is_integral<float>()()

It can be seen more clear in the following code:

    std::is_integral<int> x;
    if (x) { // operator bool
        std::cout << "true" << std::endl;
    }
    std::cout << x << std::endl;  // operator bool
    std::cout << x() << std::endl; // operator()
    std::cout << std::is_integral<int>()() << std::endl; // operator()

std::is_integral<float>() is actually an anonymous object of type std::is_integral<float>.

idealvin
  • 178
  • 1
  • 5