0

I have seen class in a library I am using, there is a use of operator overloading that I haven't seen and I can't understand. I can't call it, and it's not the typical parentheses operator overloading I've seen and used which is usually like void operator()(), with two sets of parentheses:

template <typename T, int N = 1>
class Bitmap {

  // irrelevant code excised

    T * operator()(int x, int y);
    const T * operator()(int x, int y) const;
#ifdef MSDFGEN_USE_CPP11
    explicit operator T *();
    explicit operator const T *() const;
#else
    operator T *(); // THESE ONES
    operator const T *() const; // // THESE ONES
#endif
    operator BitmapRef<T, N>(); // THESE ONES
    operator BitmapConstRef<T, N>() const; // THESE ONES


};
Useless
  • 64,155
  • 6
  • 88
  • 132
Zebrafish
  • 11,682
  • 3
  • 43
  • 119
  • 2
    https://en.cppreference.com/w/cpp/language/cast_operator – fabian Jan 03 '23 at 11:09
  • 1
    If you're wondering about `operator T *()` then it's a type-conversion operator. For example an object of type `Bitmap` can be implicitly converted to `int*` because of that operator. The `explicit` variants means the conversion must be explicit (with e.g. `static_cast(...)`). – Some programmer dude Jan 03 '23 at 11:09
  • 1
    It's not `operator()`; it's `operator T*()` etc. See how there's text between the `operator` and the `()`? I say that a bit facetiously, but it's important. Since it's so common to use the name `T` for template parameters, one might expect a phrase like `operator T*` to be searchable. And - as it turns out - [it is](https://duckduckgo.com/?q=c%2B%2B+operator+T*). As a gentle reminder, [you are expected](https://meta.stackoverflow.com/questions/261592) to try to look up existing information before asking. – Karl Knechtel Jan 03 '23 at 11:13
  • 1
    Also note that conversion operators are not only for templates. A common use-case is the conversion operator `operator bool()` which returns a `true` or `false` depending on some internal status. If you have used streams in conditions then you have used just such an [operator](https://en.cppreference.com/w/cpp/io/basic_ios/operator_bool) (assuming C++11 or later). – Some programmer dude Jan 03 '23 at 11:17

0 Answers0