2

I have just learned C++ for a little bit, and discover some special functions.

Example 1

bool operator<(const B& b1,const B& b2)
bool B::operator<(const B& b2) const
//recognized by std::sort

Example 2

MyIterator C::begin();
MyIterator begin(C& c);
//recognized by range-based loop

As far as I know, those function are specially recognized in C++.
(Furthermore, in each pair, they are somehow recognized in the same manner.)

Question

What are the list of all functions that are recognized as special?
In other words, is there any part in C++ (official) specification that summarizes the list of them + how special they are?

I believe that if I blindly code without this knowledge, I may make some silly mistake, especially when interact with std:: class.

Sorry for a not-so-sensible topic name, but I can't think of a better one.

javaLover
  • 6,347
  • 2
  • 22
  • 67
  • 1
    An old question in SO about operator overloading: http://stackoverflow.com/questions/4421706/operator-overloading. . – R Sahu Feb 13 '17 at 07:04
  • Thank! That is useful. – javaLover Feb 13 '17 at 07:06
  • A specification? Sure, do you think C++ just floats in the air? There's a [standard](http://eel.is/c++draft/); – DeiDei Feb 13 '17 at 07:06
  • 1
    In addition to the operator overload functions, constructors and destructors are special member functions. – R Sahu Feb 13 '17 at 07:07
  • @R Sahu Thank. I am also afraid of "begin/end". Are there any more of them? The link doesn't mention about it. – javaLover Feb 13 '17 at 07:08
  • @DeiDei Do you know which part summarizes them in one place? – javaLover Feb 13 '17 at 07:13
  • No, there isn't such a part. They are scattered all around. As per functions such as begin/end goes, that's about it. Then there's the operator overloading and the special member functions like constructor/destructor which were already mentioned. – DeiDei Feb 13 '17 at 07:16
  • 1
    @javaLover, `begin` and `end` are the two other special functions that were essential for the *range-for* implementation (C++11 and later). I don't recall any other special functions. – R Sahu Feb 13 '17 at 07:19
  • @DeiDei Thank for confirmation! That fact is scary, I don't know when I will step on some bombs. – javaLover Feb 13 '17 at 07:20

1 Answers1

3

IMO, following are treated specially in C++:

  • main(), which has more than 1 valid syntax
  • Constructors & Destructors
  • Certain special keywords, which are used as of like functions: 4 casts (static, dynamic, const, reinterpret) and typeid
  • begin() & end() methods or functions are specially treated while dealing with range based for loops
  • All kind of overloaded operators
  • Conversion operators e.g. struct A { operator int (); };

Few items might be missing. Not sure, if everything is listed somewhere in standard, next to impossible.

However, your fear for messing up with namespace std is misplaced. It does contain some standard functions, but unless you don't do using namespace std, there is no fear of any messing up.

Community
  • 1
  • 1
iammilind
  • 68,093
  • 33
  • 169
  • 336
  • Thank for answering my question again! On topic, I fear that `std::` will sneakily recognize some of my functions when I am not aware. – javaLover Feb 13 '17 at 07:18
  • 1
    @javaLover, that could be either because of `using namespace std` or you might be using header files where certain functions are treated like free functions even though they are supposed to be inside `std`. Are you by chance using deprecated header `` (``) instead of `` (or ``). – iammilind Feb 13 '17 at 07:21
  • No. I never using namespace std. It is just a fear after I learn about the begin() end(). If I had used that reserved names (begin/end) in my class before I know this fact, it would be a small disaster. – javaLover Feb 13 '17 at 07:24
  • 1
    @javaLover, there are certain other reserve name conventions, which you may find here: [Are identifiers starting with an underscore reserved according to the latest C++ standard?](http://stackoverflow.com/q/12924243/514235) & in its linked duplicate. But in general, C++ is very open language with many paradigms. More you go deeper into this language, more are the chances that you may feel a need to change your current username. ;-) (no offence) – iammilind Feb 13 '17 at 07:28
  • `swap` is an edge case. – MSalters Feb 13 '17 at 08:25